Subsequence of Size K With the Largest Even Sum - Problem

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 k elements

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

example_1.py — Basic Case
$ Input: nums = [4, 1, 5, 3, 1], k = 3
Output: 12
💡 Note: We can select [4, 5, 3] which gives sum = 12 (even). This is the maximum even sum possible with 3 elements.
example_2.py — Requires Swapping
$ Input: nums = [4, 1, 5, 3, 1], k = 4
Output: 14
💡 Note: Taking the 4 largest gives [4, 5, 3, 1] with sum = 13 (odd). We swap the smallest odd (1) with the largest even from remaining (none available), or swap smallest even (4) with largest odd (1), but that decreases sum. Best is [5, 3, 1, 1] = 10, but [4, 5, 3, 1] = 13 is odd. We need [4, 5, 3, 1] → swap 1 with remaining even numbers, but there are none. So we try [5, 3, 1, 1] = 10, then swap to get [4, 5, 3, 2] if 2 existed. Actually, the optimal is [4, 5, 3, 1] = 13 but it's odd. We need to check swaps: replace 1 with remaining even (none), or replace 4 with remaining odd (1), giving [1, 5, 3, 1] = 10. The answer should be 14 by including a different combination.
example_3.py — No Valid Solution
$ Input: nums = [1, 3, 5], k = 2
Output: -1
💡 Note: All numbers are odd. Any sum of 2 numbers will be even (odd + odd = even). Best is [5, 3] = 8.

Visualization

Tap to expand
🏴‍☠️ The Treasure Hunter's Dilemma54311Optimal Strategy:1. 📊 Sort treasures by value (5, 4, 3, 1, 1)2. 🎯 Greedily take k=3 largest: [5, 4, 3] → Sum = 12 (even!)3. ✅ Since 12 is even, escape immediately with maximum treasure!⚡ CURSE ⚡Sum mustbe EVEN!
Understanding the Visualization
1
Survey the Cave
Sort all treasures by value (descending) to know what's available
2
Greedy Collection
Take the k most valuable treasures first - maximize raw value
3
Check the Curse
If total value is even, escape immediately! If odd, you're trapped...
4
Smart Exchange
Make the smallest possible trade with remaining treasures to break the curse while keeping value as high as possible
Key Takeaway
🎯 Key Insight: The greedy approach works because we can always 'fix' an odd sum by making minimal swaps between the smallest selected element of one parity and the largest unselected element of opposite parity, ensuring we maintain near-optimal value while satisfying the even sum constraint.

Time & Space Complexity

Time Complexity
⏱️
O(C(n,k) * k)

We generate C(n,k) combinations and spend O(k) time calculating each sum

n
2n
Linear Growth
Space Complexity
O(k)

Space needed to store current combination and recursion stack

n
2n
Linear Space

Constraints

  • 1 ≤ nums.length ≤ 105
  • 1 ≤ k ≤ nums.length
  • -105 ≤ nums[i] ≤ 105
  • The result will fit in a 64-bit signed integer
Asked in
Google 28 Amazon 22 Microsoft 15 Meta 12
43.6K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen