Minimum Subsequence in Non-Increasing Order
EasyPrompt
Given the array nums, obtain a subsequence of the array whose sum of elements is strictly greater than the sum of the non included elements in such subsequence.
If there are multiple solutions, return the subsequence with minimum size and if there still exist multiple solutions, return the subsequence with the maximum total sum of all its elements. A subsequence of an array can be obtained by erasing some (possibly zero) elements from the array.
Note that the solution with the given constraints is guaranteed to be unique. Also return the answer sorted in non-increasing order.
Example 1:
Input: nums = [4,3,10,9,8]
Output: [10,9]
Explanation: The subsequences [10,9] and [10,8] are minimal such that the sum of their elements is strictly greater than the sum of elements not included. However, the subsequence [10,9] has the maximum total sum of its elements. Example 2:
Input: nums = [4,4,7,6,7]
Output: [7,7,6]
Explanation: The subsequence [7,7] has the sum of its elements equal to 14 which is not strictly greater than the sum of elements not included (14 = 4 + 4 + 6). Therefore, the subsequence [7,6,7] is the minimal satisfying the conditions. Note the subsequence has to be returned in non-increasing order.
Constraints:
1 <= nums.length <= 5001 <= nums[i] <= 100
Approaches
3 approaches with complexity analysis and trade-offs.
This naive approach involves generating every possible non-empty subsequence of the input array. For each subsequence, we check if its sum is strictly greater than the sum of the elements not included. Among all such valid subsequences, we find the one that first minimizes the size, and then maximizes the sum. This method is exhaustive but computationally very expensive.
Algorithm
- Generate all 2^N - 1 non-empty subsequences of the
numsarray. - Calculate the
totalSumof thenumsarray. - Create a list to store valid candidate subsequences.
- For each generated subsequence:
- Calculate its sum,
subsequenceSum. - If
subsequenceSum > totalSum - subsequenceSum, add it to the list of candidates.
- Calculate its sum,
- Sort the candidates first by size (ascending) and then by sum (descending).
- The first subsequence in the sorted list is the answer.
- Sort this final subsequence in non-increasing order before returning.
Walkthrough
The core idea is to test every single possibility. We can use a recursive backtracking function or bit manipulation to generate all subsequences. For an array of size N, there are 2^N subsequences. For each one, we compute its sum and the sum of the remaining elements to check the condition. We store all subsequences that satisfy subsequence_sum > remaining_sum. Finally, we iterate through these valid subsequences to find the one with the minimum size. If there's a tie in size, we pick the one with the largest sum. Due to its exponential complexity, this approach is not feasible for the given constraints but serves as a foundational, albeit impractical, solution.
Complexity
Time
O(N * 2^N), where N is the number of elements in `nums`. Generating 2^N subsequences and processing each one takes O(N) time.
Space
O(N * 2^N), required to store all possible subsequences for processing.
Trade-offs
Pros
Guarantees finding the correct solution by checking every possibility.
Conceptually straightforward.
Cons
Extremely inefficient and will result in a 'Time Limit Exceeded' error for the given constraints.
Requires a large amount of memory.
Solutions
Solution
class Solution {public List<Integer> minSubsequence(int[] nums) { Arrays.sort(nums); List<Integer> ans = new ArrayList<>(); int s = Arrays.stream(nums).sum(); int t = 0; for (int i = nums.length - 1; i >= 0; i--) { t += nums[i]; ans.add(nums[i]); if (t > s - t) { break; } } return ans; }}Video walkthrough
Newsletter
One sharp idea, every week
System design and interview prep — short enough to finish.
No spam. Unsubscribe anytime.
Practice
Same difficulty — related problems to reinforce the pattern.