Problem Description
Given an array nums, obtain a subsequence of the array such that the sum of its elements is strictly greater than the sum of the remaining elements. The chosen subsequence should have the minimum possible size; if there are multiple solutions with minimal size, return the one with the maximum total sum. The final subsequence must be returned in non-increasing order.
Key Insights
- Sorting the array in descending order helps in greedily choosing the largest elements first.
- Greedily adding the largest elements ensures that we minimize the number of elements needed and maximize the subsequence sum.
- Keep track of the running sum of chosen elements and the remaining total to check when the subsequence sum becomes strictly greater.
Space and Time Complexity
Time Complexity: O(n log n) due to sorting. Space Complexity: O(n) for storing the sorted array and the resulting subsequence.
Solution
The approach involves:
- Sorting the input array in non-increasing order.
- Iterating through the sorted array and accumulating the sum of selected elements.
- After each selection, subtract the element’s value from the total remaining sum and check if the current subsequence sum exceeds it.
- Once the condition is met, the chosen elements form the required subsequence.
This greedy approach ensures the solution is minimal in size and, among minimal choices, has the maximum sum.