You are given an integer array nums and an integer k.
Return the largest even sum of a subsequence of nums that has size k. If there is no such subsequence, return -1.
A subsequence of an array is a sequence that can be derived from the array by deleting some or no elements without changing the order of the remaining elements.
Example 1:
Input: nums = [4,1,5,3,2], k = 3 Output: 12 Explanation: The subsequence with the largest even sum is [4,3,5] which has a sum of 12.
Example 2:
Input: nums = [4,6,2], k = 3 Output: 12 Explanation: The subsequence with the largest even sum is [4,6,2] which has a sum of 12.
Example 3:
Input: nums = [1,3,5], k = 1 Output: -1 Explanation: There is no subsequence of size 1 that has an even sum.
Constraints:
1 <= nums.length <= 1050 <= nums[i] <= 1051 <= k <= nums.lengthWhen you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
This problem asks us to find a way to pick a specific number of items from a larger collection so that the sum of the chosen items is even and as large as possible. The brute force method involves trying out every single possible combination of items we could pick.
Here's how the algorithm would work step-by-step:
import itertoolsdef find_largest_even_subsequence_sum(numbers, subsequence_size): largest_even_sum_found = -1 all_combinations = itertools.combinations(numbers, subsequence_size) # We need to iterate through every possible group of numbers to check their sums. for current_combination in all_combinations: current_sum = sum(current_combination) # Ensure the sum is even before considering it as a potential largest even sum. if current_sum % 2 == 0: # If the current even sum is greater than what we've seen, update our record. if current_sum > largest_even_sum_found: largest_even_sum_found = current_sum # If no even sum was found, it remains -1. Otherwise, it holds the largest one. return largest_even_sum_foundThe best way to find the largest even sum subsequence of a specific size is to first consider the largest possible numbers. We then strategically swap out numbers to ensure the sum is even while keeping it as large as possible.
Here's how the algorithm would work step-by-step:
def largest_even_sum_subsequence(input_numbers, subsequence_size):
even_numbers = sorted([num for num in input_numbers if num % 2 == 0], reverse=True)
odd_numbers = sorted([num for num in input_numbers if num % 2 != 0], reverse=True)
current_subsequence = []
current_sum = 0
# Greedily pick largest numbers first to maximize sum
even_index = 0
odd_index = 0
while len(current_subsequence) < subsequence_size:
if even_index < len(even_numbers) and odd_index < len(odd_numbers):
if even_numbers[even_index] > odd_numbers[odd_index]:
current_subsequence.append(even_numbers[even_index])
current_sum += even_numbers[even_index]
even_index += 1
else:
current_subsequence.append(odd_numbers[odd_index])
current_sum += odd_numbers[odd_index]
odd_index += 1
elif even_index < len(even_numbers):
current_subsequence.append(even_numbers[even_index])
current_sum += even_numbers[even_index]
even_index += 1
elif odd_index < len(odd_numbers):
current_subsequence.append(odd_numbers[odd_index])
current_sum += odd_numbers[odd_index]
odd_index += 1
else:
# Not enough numbers to form the subsequence
return -1
# If the initial sum is even, we found the optimal subsequence
if current_sum % 2 == 0:
return current_sum
# If the sum is odd, we need to make swaps to make it even
# The goal is to minimize the reduction in sum while achieving an even sum.
min_sum_reduction = float('inf')
best_even_sum = -1
# Try swapping smallest odd in subsequence with largest even out of subsequence
smallest_odd_in = float('inf')
index_smallest_odd_in = -1
for i, num in enumerate(current_subsequence):
if num % 2 != 0 and num < smallest_odd_in:
smallest_odd_in = num
index_smallest_odd_in = i
largest_even_out = -1
if even_index < len(even_numbers):
largest_even_out = even_numbers[even_index]
if smallest_odd_in != float('inf') and largest_even_out != -1:
potential_sum = current_sum - smallest_odd_in + largest_even_out
if potential_sum % 2 == 0:
reduction = smallest_odd_in - largest_even_out
if reduction < min_sum_reduction:
min_sum_reduction = reduction
best_even_sum = potential_sum
# Try swapping smallest even in subsequence with largest odd out of subsequence
smallest_even_in = float('inf')
index_smallest_even_in = -1
for i, num in enumerate(current_subsequence):
if num % 2 == 0 and num < smallest_even_in:
smallest_even_in = num
index_smallest_even_in = i
largest_odd_out = -1
if odd_index < len(odd_numbers):
largest_odd_out = odd_numbers[odd_index]
if smallest_even_in != float('inf') and largest_odd_out != -1:
potential_sum = current_sum - smallest_even_in + largest_odd_out
if potential_sum % 2 == 0:
reduction = smallest_even_in - largest_odd_out
if reduction < min_sum_reduction:
min_sum_reduction = reduction
best_even_sum = potential_sum
return best_even_sum
| Case | How to Handle |
|---|---|
| k is larger than the number of elements in nums | The problem constraints imply k will not exceed nums. If it could, return -1 as no subsequence of size k can be formed. |
| nums is empty or null | Return -1 immediately if nums is null or empty, as no subsequence can be formed. |
| k is zero or negative | Return -1 if k is zero or negative, as a subsequence of non-positive size is not meaningful in this context. |
| No subsequence of size k can form an even sum | The algorithm will naturally result in -1 if no combination of k elements yields an even sum. |
| All numbers in nums are odd | If k is odd, an even sum is impossible; if k is even, an even sum might be possible using pairs of odd numbers. |
| All numbers in nums are even | Any subsequence of size k will have an even sum, so we need to pick the k largest numbers. |
| nums contains zeros | Zeros do not affect the parity of the sum but can be part of the largest sum subsequence if they are among the largest elements. |
| nums contains negative numbers | Negative numbers can be included if they contribute to the largest possible even sum, requiring careful sorting and selection. |