413. Subsequence of Size K With the Largest Even Sum

You are given an integer array nums and an integer k. Find the largest even sum of any subsequence of nums that has a length of k.

Return this sum, or -1 if such a sum does not exist.

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 = [4,1,5,3,1], k = 3

Output: 12

Explanation:

The subsequence with the largest possible even sum is [4,5,3]. It has a sum of 4 + 5 + 3 = 12.

Example 2:

Input: nums = [4,6,2], k = 3

Output: 12

Explanation:

The subsequence with the largest possible even sum is [4,6,2]. It has a sum of 4 + 6 + 2 = 12.

Now Your Turn!

Pick the correct output for the given input

Consider the array nums = [2,4,7,8,10] and k = 2. What is the largest possible even sum of a subsequence of length k?

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= nums.length <= 105
  • 0 <= nums[i] <= 105
  • 1 <= k <= nums.length

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
long long largestEvenSum(vector<int>& nums, int k) {
// Your code goes here
}
};
Test Case

Input:

K
Nums