Statement▼
You are given an integer array, nums
, and a positive integer k
. Your task is to determine and return the
Remember: For valid subsequences:
The empty subsequence is valid, and its sum is considered
0 .Duplicate subsequence sums are allowed and counted separately when determining the
kth largest.
Constraints:
n== nums.length
1≤ n
≤103 −103≤ nums[i]
≤103 1<= k
Solution
A brute-force approach to this problem will involve generating all possible subsequences of the array. Since an array of length n has exactly n
(up to 1000).
To solve the problem efficiently, we reframe the task: instead of directly computing the
Thus, finding the
To compute these losses, we transform the problem as follows:
Convert all elements to absolute values—as excluding positives or including negatives both reduce the total.
Sort this array to prioritize smaller losses.
Use a min heap to systematically explore the smallest reduction combinations.
We initialize the heap with (loss =
Including the current value: (currLoss + nums[i], i+1)