##### Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/1403.html

# 1403. Minimum Subsequence in Non-Increasing Order (Easy)

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.

Example 1:

Input: nums = [4,3,10,9,8]
Output: [10,9]
Explanation: 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, however, the subsequence [10,9] has the maximum total sum of its elements.


Example 2:

Input: nums = [4,4,7,6,7]
Output: [7,7,6]
Explanation: The subsequence [7,7] has the sum of its elements equal to 14 which is not strictly greater than the sum of elements not included (14 = 4 + 4 + 6). Therefore, the subsequence [7,6,7] is the minimal satisfying the conditions. Note the subsequence has to returned in non-decreasing order.


Example 3:

Input: nums = 
Output: 


Constraints:

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

Related Topics:
Greedy, Sort

## Solution 1.

• class Solution {
public List<Integer> minSubsequence(int[] nums) {
int sum = 0;
for (int num : nums)
sum += num;
int halfSum = sum / 2;
Arrays.sort(nums);
List<Integer> list = new ArrayList<Integer>();
int subsum = 0;
for (int i = nums.length - 1; i >= 0; i--) {
int num = nums[i];
subsum += num;
if (subsum > halfSum)
break;
}
return list;
}
}

############

class Solution {
public List<Integer> minSubsequence(int[] nums) {
Arrays.sort(nums);
List<Integer> ans = new ArrayList<>();
int s = 0;
for (int v : nums) {
s += v;
}
int t = 0;
for (int i = nums.length - 1; i >= 0; --i) {
t += nums[i];
if (t > s - t) {
break;
}
}
return ans;
}
}

• // OJ: https://leetcode.com/problems/minimum-subsequence-in-non-increasing-order/
// Time: O(NlogN)
// Space: O(1)
class Solution {
public:
vector<int> minSubsequence(vector<int>& A) {
sort(A.begin(), A.end(), greater<int>());
int sum = accumulate(begin(A), end(A), 0);
int s = 0;
vector<int> ans;
for (int i = 0; i < A.size(); ++i) {
s += A[i];
ans.push_back(A[i]);
if (s > sum - s) break;
}
return ans;
}
};

• class Solution:
def minSubsequence(self, nums: List[int]) -> List[int]:
nums.sort(reverse=True)
s = sum(nums)
ans = []
t = 0
for v in nums:
ans.append(v)
t += v
if t > s - t:
break
return ans


• func minSubsequence(nums []int) []int {
sort.Ints(nums)
s, t := 0, 0
for _, v := range nums {
s += v
}
ans := []int{}
for i := len(nums) - 1; i >= 0; i-- {
t += nums[i]
ans = append(ans, nums[i])
if t > s-t {
break
}
}
return ans
}

• function minSubsequence(nums: number[]): number[] {
nums.sort((a, b) => b - a);
const sum = nums.reduce((r, c) => r + c);
const res: number[] = [];
let t = 0;
for (const num of nums) {
t += num;
res.push(num);
if (t > sum - t) {
break;
}
}
return res;
}


• impl Solution {
pub fn min_subsequence(mut nums: Vec<i32>) -> Vec<i32> {
nums.sort_by(|a, b| b.cmp(a));
let sum = nums.iter().sum::<i32>();
let mut res = vec![];
let mut t = 0;
for num in nums.into_iter() {
t += num;
res.push(num);
if t > sum - t {
break;
}
}
res
}
}