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 <= 105
0 <= nums[i] <= 105
1 <= k <= nums.length
When 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:
The brute force approach is all about trying absolutely everything! For this problem, we need to find the best group of numbers of a specific size that adds up to an even number. We will explore every possible combination of numbers until we find the best one.
Here's how the algorithm would work step-by-step:
def find_largest_even_sum_brute_force(numbers, k):
all_even_sums = []
number_of_numbers = len(numbers)
# Iterate through all possible combinations of k numbers
for i in range(1 << number_of_numbers):
current_combination = []
for j in range(number_of_numbers):
if (i >> j) & 1:
current_combination.append(numbers[j])
if len(current_combination) == k:
sum_of_combination = sum(current_combination)
# Check if the sum is even
if sum_of_combination % 2 == 0:
# Store the even sum
all_even_sums.append(sum_of_combination)
# Find the largest even sum from all combinations
if not all_even_sums:
return -1
else:
return max(all_even_sums)
The goal is to find the 'k' numbers from the given list that add up to the largest possible even number. We achieve this by prioritizing even numbers and strategically handling odd numbers to ensure the sum remains even and maximized.
Here's how the algorithm would work step-by-step:
def find_largest_even_subsequence(number_list, subsequence_size):
even_numbers = sorted([number for number in number_list if number % 2 == 0], reverse=True)
odd_numbers = sorted([number for number in number_list if number % 2 != 0], reverse=True)
max_sum = -1
# Check if we can form a subsequence using only even numbers.
if len(even_numbers) >= subsequence_size:
max_sum = sum(even_numbers[:subsequence_size])
# Iterate to find the best combination of even and odd numbers.
for odd_numbers_count in range(min(len(odd_numbers) + 1, subsequence_size + 1)):
if odd_numbers_count % 2 == 0:
continue
#Ensure total number of items does not exceed the list
even_numbers_count = subsequence_size - odd_numbers_count
if even_numbers_count < 0 or even_numbers_count > len(even_numbers):
continue
current_sum = sum(odd_numbers[:odd_numbers_count]) + sum(even_numbers[:even_numbers_count])
max_sum = max(max_sum, current_sum)
for odd_numbers_count in range(min(len(odd_numbers) + 1, subsequence_size + 1)):
if odd_numbers_count % 2 != 0:
continue
even_numbers_count = subsequence_size - odd_numbers_count
# Ensure we don't take more evens than there are, or negative
if even_numbers_count < 0 or even_numbers_count > len(even_numbers):
continue
# This iteration ensures sum is still even.
current_sum = sum(odd_numbers[:odd_numbers_count]) + sum(even_numbers[:even_numbers_count])
max_sum = max(max_sum, current_sum)
# Return -1 if no valid subsequence can be found
if max_sum == -1:
return -1
return max_sum
Case | How to Handle |
---|---|
Null or empty input array nums | Return -1 immediately as no subsequence can be formed. |
k is 0 | Return 0 since an empty subsequence has sum 0, which is even. |
k is greater than the length of nums | Return -1 immediately, as a subsequence of length k cannot be formed. |
Array contains only odd numbers and k is odd | No even sum is possible; return -1 if no even subsequence sum can be created. |
Array contains only odd numbers and k is even | No even sum is possible; return -1. |
Array contains only negative odd numbers and k is even | Return the largest possible negative even sum if k <= len(nums), and -1 otherwise. |
Array contains only negative odd numbers and k is odd | Return -1 as no positive or zero result is possible and the question requires a maximum even sum. |
Integer overflow if sum is very large | Use long data type to store intermediate sums. |