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
Understanding the Visualization
1
Input Array
Given array with total sum calculation
2
Find Dominant Subsequence
Select minimum elements whose sum > remaining sum
3
Output
Return subsequence sorted in non-increasing order
Key Takeaway
🎯 Key Insight: Greedily pick the largest elements first to minimize subsequence size while maximizing sum
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code