LC 1403. Minimum Subsequence in Non-Increasing Order

Nilanjan Deb · April 6, 2020

This is my CPP solution.

class Solution {
public:
    vector<int> minSubsequence(vector<int>& nums) {
        sort(nums.begin(),nums.end());
        int n = nums.size();
        vector<int> res;
        int sum = 0,current_sum = 0;
        for(int a:nums)
            sum += a;
        for(int i = n-1;i>=0;i--){
            sum-=nums[i];
            current_sum += nums[i];
            res.push_back(nums[i]);
            if(current_sum > sum){
                break;
            }
        }
        return res;
    }
};


Dicussion Forum