Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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 <= 5001 <= nums[i] <= 100Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[4,3,10,9,8]
[4,4,7,6,7]
count-hills-and-valleys-in-an-array)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1403: Minimum Subsequence in Non-Increasing Order
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;
}
}
// Accepted solution for LeetCode #1403: Minimum Subsequence in Non-Increasing Order
func minSubsequence(nums []int) (ans []int) {
sort.Ints(nums)
s, t := 0, 0
for _, x := range nums {
s += x
}
for i := len(nums) - 1; ; i-- {
t += nums[i]
ans = append(ans, nums[i])
if t > s-t {
return
}
}
}
# Accepted solution for LeetCode #1403: Minimum Subsequence in Non-Increasing Order
class Solution:
def minSubsequence(self, nums: List[int]) -> List[int]:
ans = []
s, t = sum(nums), 0
for x in sorted(nums, reverse=True):
t += x
ans.append(x)
if t > s - t:
break
return ans
// Accepted solution for LeetCode #1403: Minimum Subsequence in Non-Increasing Order
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
}
}
// Accepted solution for LeetCode #1403: Minimum Subsequence in Non-Increasing Order
function minSubsequence(nums: number[]): number[] {
nums.sort((a, b) => b - a);
const s = nums.reduce((r, c) => r + c);
let t = 0;
for (let i = 0; ; ++i) {
t += nums[i];
if (t > s - t) {
return nums.slice(0, i + 1);
}
}
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.