Given an integer array nums and an integer k, you need to find a subsequence of exactly k elements that produces the largest even sum.
A subsequence maintains the relative order of elements from the original array, but you can skip any elements you don't need. Your goal is to select exactly k numbers that:
- ✅ Have the maximum possible sum
- ✅ The sum is even
- ✅ Contain exactly
kelements
Return the largest even sum possible, or -1 if no such subsequence exists.
Example: For nums = [4, 1, 5, 3, 1] and k = 3, you could pick [4, 5, 3] with sum = 12 (even), but the optimal answer is [4, 5, 1] with sum = 10 (even and maximum possible even sum with 3 elements).
Input & Output
Visualization
Time & Space Complexity
We generate C(n,k) combinations and spend O(k) time calculating each sum
Space needed to store current combination and recursion stack
Constraints
- 1 ≤ nums.length ≤ 105
- 1 ≤ k ≤ nums.length
- -105 ≤ nums[i] ≤ 105
- The result will fit in a 64-bit signed integer