Minimum Subsequence in Non-Increasing Order - Problem

๐ŸŽฏ Find the Minimum Subsequence with Maximum Impact

You're given an array nums and need to find a special subsequence that packs the biggest punch with the smallest size!

Your mission: Find a subsequence where the sum of its elements is strictly greater than the sum of all the elements you left behind.

The twist: If multiple subsequences work, you want:

  1. ๐Ÿ† Minimum size (fewer elements preferred)
  2. ๐Ÿ’ฐ Maximum sum (if sizes are equal, pick the one with larger total)
  3. ๐Ÿ“‰ Non-increasing order (return result sorted from largest to smallest)

Example: Given [4,3,10,9,8], you could pick [10,9] with sum 19, which is greater than the remaining sum [4,3,8] = 15. Perfect! โœจ

The problem guarantees a unique solution exists, so no need to worry about ties!

Input & Output

example_1.py โ€” Basic Case
$ Input: [4,3,10,9,8]
โ€บ Output: [10,9]
๐Ÿ’ก Note: The sum of [10,9] is 19, which is greater than the sum of remaining elements [4,3,8] = 15. This is the minimum size subsequence that satisfies the condition.
example_2.py โ€” Tie-breaking
$ Input: [4,4,7,6,7]
โ€บ Output: [7,7,6]
๐Ÿ’ก Note: Multiple subsequences work, but [7,7,6] has sum 20 > remaining 8, and it's the optimal choice following our criteria (minimum size, maximum sum).
example_3.py โ€” Single Element
$ Input: [6]
โ€บ Output: [6]
๐Ÿ’ก Note: With only one element, we must take it. Sum 6 > remaining 0, so [6] is the answer.

Constraints

  • 1 โ‰ค nums.length โ‰ค 500
  • 1 โ‰ค nums[i] โ‰ค 100
  • The answer is guaranteed to be unique
  • The subsequence must be returned in non-increasing order

Visualization

Tap to expand
๐Ÿดโ€โ˜ ๏ธ The Treasure Hunter's StrategyCave Treasure: [4๐Ÿ’ฐ, 3๐Ÿ’ฐ, 10๐Ÿ’ฐ, 9๐Ÿ’ฐ, 8๐Ÿ’ฐ] Total: 34๐Ÿ’ฐStep 1: Arrange by Value109843Step 2-3: Greedy Selection Process๐Ÿ‘œ Take coin 10: My treasure = 10๐Ÿ’ฐ, Remaining = 24๐Ÿ’ฐ Check: 10 > 24? โŒ Need more coins!๐Ÿ‘œ Take coin 9: My treasure = 19๐Ÿ’ฐ, Remaining = 15๐Ÿ’ฐ Check: 19 > 15? โœ… Mission accomplished!Final Result๐ŸŽฏ Optimal Solution: [10, 9]โœจ Minimum coins (2), Maximum value (19), Non-increasing order๐Ÿ’ก Why This Works๐Ÿง  Greedy Insight:Taking the most valuable coinsfirst minimizes the total countneeded to exceed the threshold.โšก Time Complexity:O(n log n) for sortingO(n) for greedy selection๐ŸŽฏ Space: O(n) for result
Understanding the Visualization
1
Arrange by Value
Line up all treasure coins from most valuable to least valuable
2
Greedy Selection
Pick coins one by one, always choosing the most valuable remaining coin
3
Check Progress
After each coin, check if you have more than the remaining treasure
4
Stop Condition
Stop as soon as your coins are worth more than what's left behind
Key Takeaway
๐ŸŽฏ Key Insight: The greedy approach works because taking the largest elements first always leads to the minimum subsequence size needed to satisfy the sum condition, automatically maximizing the sum for any given size.
Asked in
Amazon 15 Google 8 Microsoft 5 Meta 3
28.4K Views
Medium Frequency
~12 min Avg. Time
892 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