Problem · Array

Maximize Sum of Array

Learn this problem
MediumAmazonOA
See Amazon hiring insights

Problem statement

You are given an array of integers. Your task is to maximize the sum by performing the following operations repeatedly until the array is empty:

  1. Choose any element from the current array that hasn't been removed before.
  2. Add this element to your total sum.
  3. Remove the first and last elements of the array.

Return the maximum sum you can achieve following these steps.

Function

maximizeSum(arr: int[]) → int

Examples

Example 1

arr = [4, 4, 8, 5, 3, 2]return = 17

Steps:

  1. Choose 5 from [4, 4, 8, 5, 3, 2]
  2. Remove 4 and 2
  3. Total sum = 5
  4. Choose 4 from [4, 8, 5, 3]
  5. Remove 4 and 3
  6. Total sum = 5 + 4 = 9
  7. Choose 8 from [8, 5]
  8. Remove 8 and 5
  9. Total sum = 9 + 8 = 17

Constraints

🐳

More Amazon problems

drafts saved locally
public int maximizeSum(int[] arr) {
  // write your code here
}
arr[4, 4, 8, 5, 3, 2]
expected17
checking account