Problem · Dynamic Programming
Maximum Even Sum Subsequence
Learn this problemProblem statement
Given an array arr containing positive and negative integers, choose a subsequence whose sum is even and as large as possible.
The subsequence may be empty. The empty subsequence has sum 0.
Return the maximum possible even sum.
Function
maximumEvenSumSubsequence(arr: int[]) → intExamples
Example 1
arr = [-2, 2, -3, 1, 3]return = 6Choose the subsequence [2, 1, 3]. Its sum is 6, which is the largest achievable even sum.
Example 2
arr = [-2, 2, -3, 4, 5]return = 8Choose the subsequence [2, -3, 4, 5]. Its sum is 8, which is the largest achievable even sum.