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.
You are given an integer array nums and an integer k. You want to find a subsequence of nums of length k that has the largest sum.
Return any such subsequence as an integer array of length k.
A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
Example 1:
Input: nums = [2,1,3,3], k = 2 Output: [3,3] Explanation: The subsequence has the largest sum of 3 + 3 = 6.
Example 2:
Input: nums = [-1,-2,3,4], k = 3 Output: [-1,3,4] Explanation: The subsequence has the largest sum of -1 + 3 + 4 = 6.
Example 3:
Input: nums = [3,4,3,3], k = 2 Output: [3,4] Explanation: The subsequence has the largest sum of 3 + 4 = 7. Another possible subsequence is [4, 3].
Constraints:
1 <= nums.length <= 1000-105 <= nums[i] <= 1051 <= k <= nums.lengthProblem summary: You are given an integer array nums and an integer k. You want to find a subsequence of nums of length k that has the largest sum. Return any such subsequence as an integer array of length k. A subsequence is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[2,1,3,3] 2
[-1,-2,3,4] 3
[3,4,3,3] 2
kth-largest-element-in-an-array)maximize-sum-of-array-after-k-negations)sort-integers-by-the-number-of-1-bits)minimum-difference-in-sums-after-removal-of-elements)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2099: Find Subsequence of Length K With the Largest Sum
class Solution {
public int[] maxSubsequence(int[] nums, int k) {
int n = nums.length;
Integer[] idx = new Integer[n];
Arrays.setAll(idx, i -> i);
Arrays.sort(idx, (i, j) -> nums[i] - nums[j]);
Arrays.sort(idx, n - k, n);
int[] ans = new int[k];
for (int i = n - k; i < n; ++i) {
ans[i - (n - k)] = nums[idx[i]];
}
return ans;
}
}
// Accepted solution for LeetCode #2099: Find Subsequence of Length K With the Largest Sum
func maxSubsequence(nums []int, k int) []int {
idx := slices.Clone(make([]int, len(nums)))
for i := range idx {
idx[i] = i
}
slices.SortFunc(idx, func(i, j int) int { return nums[i] - nums[j] })
slices.Sort(idx[len(idx)-k:])
ans := make([]int, k)
for i := range ans {
ans[i] = nums[idx[len(idx)-k+i]]
}
return ans
}
# Accepted solution for LeetCode #2099: Find Subsequence of Length K With the Largest Sum
class Solution:
def maxSubsequence(self, nums: List[int], k: int) -> List[int]:
idx = sorted(range(len(nums)), key=lambda i: nums[i])[-k:]
return [nums[i] for i in sorted(idx)]
// Accepted solution for LeetCode #2099: Find Subsequence of Length K With the Largest Sum
impl Solution {
pub fn max_subsequence(nums: Vec<i32>, k: i32) -> Vec<i32> {
let n = nums.len();
let k = k as usize;
let mut idx: Vec<usize> = (0..n).collect();
idx.sort_by_key(|&i| nums[i]);
idx[n - k..].sort();
let mut ans = Vec::with_capacity(k);
for i in n - k..n {
ans.push(nums[idx[i]]);
}
ans
}
}
// Accepted solution for LeetCode #2099: Find Subsequence of Length K With the Largest Sum
function maxSubsequence(nums: number[], k: number): number[] {
const n = nums.length;
const idx: number[] = Array.from({ length: n }, (_, i) => i);
idx.sort((i, j) => nums[i] - nums[j]);
return idx
.slice(n - k)
.sort((i, j) => i - j)
.map(i => nums[i]);
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
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: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.