Problem · Array
Maximum Even Tag Sum
Learn this problemProblem statement
A shop has n tags, each with a value val[i], which may be positive or negative.
A customer wants to choose a set of tags such that:
- The sum of the chosen tag values is even.
- The sum is as large as possible.
Additional notes:
- There is at least one tag with an even value.
- You may choose any number of tags, including zero.
Your task is to determine the maximum possible even sum that can be formed using any subset of the given tags.
Return this maximum even sum.
Function
maximumEvenSum(val: int[]) → intExamples
Example 1
val = [2, 3, 6, -6, 10, 1, 1]return = 22One valid choice is [2, 3, 6, 10, 1], whose sum is 22. Choosing both 1 tags would make the positive-tag sum odd, and adding -6 would only reduce the sum, so the maximum even sum is 22.