Minimum Subsequence in Non-Increasing Order

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. 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
Inputnums = [4,3,10,9,8]
Output[10,9]
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, but [10, 9] has the maximum total sum.
Example 2
Inputnums = [4,4,7,6,7]
Output[7,7,6]
The subsequence [7, 7] has sum 14, which is not strictly greater than the sum of elements not included, so [7, 7, 6] is the minimal satisfying subsequence returned in non-increasing order.

Constraints

  • 1 <= nums.length <= 500
  • 1 <= nums[i] <= 100

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate