Problem · Dynamic Programming

Maximum Even Sum Subsequence

Learn this problem
MediumMathWorksFULLTIMEOA

Problem 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[]) → int

Examples

Example 1

arr = [-2, 2, -3, 1, 3]return = 6

Choose 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 = 8

Choose the subsequence [2, -3, 4, 5]. Its sum is 8, which is the largest achievable even sum.

More MathWorks problems

drafts saved locally
public int maximumEvenSumSubsequence(int[] arr) {
  // write your code here
}
arr[-2, 2, -3, 1, 3]
expected6
checking account