Minimum Subsequence in Non-Increasing Order - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: nums = [4,3,10,9]
Output: [10,9]
💡 Note: Total sum = 26. We need subsequence sum > remaining sum. Taking [10,9] gives sum=19 > remaining sum=7. This is minimum size (2) and already sorted in non-increasing order.
Example 2 — Single Element
$ Input: nums = [4,4,7,6,7]
Output: [7,7,6]
💡 Note: Total sum = 28. Need sum > 14. Taking largest elements [7,7,6] gives sum=20 > remaining sum=8. This achieves the required condition with minimum size.
Example 3 — Edge Case
$ Input: nums = [6]
Output: [6]
💡 Note: With only one element, we must take it. Sum=6 > remaining sum=0, so [6] is the answer.

Constraints

  • 1 ≤ nums.length ≤ 500
  • 1 ≤ nums[i] ≤ 100

Visualization

Tap to expand
Minimum Subsequence in Non-Increasing Order INPUT nums = [4, 3, 10, 9] 4 idx 0 3 idx 1 10 idx 2 9 idx 3 Total Sum: 4 + 3 + 10 + 9 = 26 Goal: Find subsequence with sum strictly greater than remaining elements sum Need: subseq_sum > 13 (half of 26) ALGORITHM STEPS 1 Sort Descending [10, 9, 4, 3] 10 9 4 3 2 Pick Largest: 10 sum=10, rest=16 10 < 16 (not enough) 3 Add Next: 9 sum=19, rest=7 19 > 7 (OK - done!) 4 Return Result Already sorted desc Greedy Progress: Step 1: [10] sum=10 Step 2: [10,9] sum=19 OK FINAL RESULT Minimum Subsequence: 10 9 Output: [10, 9] Verification: Subsequence sum: 19 Remaining sum: 7 19 > 7? OK - Valid! Min size: 2 elements Max sum: 19 (greedy) Key Insight: The greedy approach works because taking the largest elements first guarantees the minimum subsequence size with maximum sum. Sort descending, then greedily pick elements until selected sum exceeds remaining sum. Time: O(n log n) for sorting. Space: O(1) extra. TutorialsPoint - Minimum Subsequence in Non-Increasing Order | Greedy Approach
Asked in
Amazon 15 Microsoft 8
28.5K Views
Medium Frequency
~12 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen