Minimum Subsequence in Non-Increasing Order

EASY

Description

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 be returned in non-increasing order.  

 

Constraints:

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

Approaches

Checkout 3 different approaches to solve Minimum Subsequence in Non-Increasing Order. Click on different approaches to view the approach and algorithm in detail.

Brute-Force by Generating All Subsequences

This naive approach involves generating every possible non-empty subsequence of the input array. For each subsequence, we check if its sum is strictly greater than the sum of the elements not included. Among all such valid subsequences, we find the one that first minimizes the size, and then maximizes the sum. This method is exhaustive but computationally very expensive.

Algorithm

  • Generate all 2^N - 1 non-empty subsequences of the nums array.
  • Calculate the totalSum of the nums array.
  • Create a list to store valid candidate subsequences.
  • For each generated subsequence:
    • Calculate its sum, subsequenceSum.
    • If subsequenceSum > totalSum - subsequenceSum, add it to the list of candidates.
  • Sort the candidates first by size (ascending) and then by sum (descending).
  • The first subsequence in the sorted list is the answer.
  • Sort this final subsequence in non-increasing order before returning.

The core idea is to test every single possibility. We can use a recursive backtracking function or bit manipulation to generate all subsequences. For an array of size N, there are 2^N subsequences. For each one, we compute its sum and the sum of the remaining elements to check the condition. We store all subsequences that satisfy subsequence_sum > remaining_sum. Finally, we iterate through these valid subsequences to find the one with the minimum size. If there's a tie in size, we pick the one with the largest sum. Due to its exponential complexity, this approach is not feasible for the given constraints but serves as a foundational, albeit impractical, solution.

Complexity Analysis

Time Complexity: O(N * 2^N), where N is the number of elements in `nums`. Generating 2^N subsequences and processing each one takes O(N) time.Space Complexity: O(N * 2^N), required to store all possible subsequences for processing.

Pros and Cons

Pros:
  • Guarantees finding the correct solution by checking every possibility.
  • Conceptually straightforward.
Cons:
  • Extremely inefficient and will result in a 'Time Limit Exceeded' error for the given constraints.
  • Requires a large amount of memory.

Code Solutions

Checking out 3 solutions in different languages for Minimum Subsequence in Non-Increasing Order. Click on different languages to view the code.

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

Video Solution

Watch the video walkthrough for Minimum Subsequence in Non-Increasing Order



Algorithms:

Sorting

Patterns:

Greedy

Data Structures:

Array

Companies:

Subscribe to Scale Engineer newsletter

Learn about System Design, Software Engineering, and interview experiences every week.

No spam, unsubscribe at any time.