Choose a subsequence of length whose elements have the maximum possible sum, while keeping the selected numbers in their original relative order.
Given an integer array nums and an integer k, return a subsequence of nums of length k such that:
k.A subsequence is formed by deleting zero or more elements without changing the order of the remaining elements.
k elements.nums.k.You may assume the array and k are provided in the usual coding-interview format.
nums with length k and maximum possible sum, preserving original order.nums may be negative, zero, or positiveExample 1
Input
nums = [2, 1, 3, 3], k = 2
Output
[3, 3]
Explanation
The maximum sum subsequence of length 2 uses the two largest values, which are both 3. Their original order is preserved.
Example 2
Input
nums = [-1, -2, 3, 4], k = 2
Output
[3, 4]
Explanation
The best choice is the subsequence with sum 7. The order is already valid in the original array.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.