Given an array **arr[]**, the task is to maximize the sum of even-indexed elements by reversing a subarray and print the maximum sum obtained.

**Examples:**

Input:arr[] = {1, 2, 1, 2, 1}

Output:5

Explanation:

Sum of intial even-indexed elements = a[0] + a[2] + a[4] = 1 + 1 + 1 = 3

Reversing subarray {1, 2, 1, 2} modifies the array to {2, 1, 2, 1, 1}.

Hence, the maximized sum = 2 + 2 + 1 = 5

Input:arr[] = {7, 8, 4, 5, 7, 6, 8, 9, 7, 3}

Output:37

**Naive Approach:**

The simplest approach to solve the problem is to generate all the possible permutations by reversal of elements one by one and calculate the sum at even indices for each permutation. Print the maximum possible sum among all the permutations.

**Time Complexity: **O(N^{3})

**Auxiliary Space:** O(N)

**Efficient Approach: **

The above approach can be further optimized to * O(N) computational complexity* by using Dynamic Programming to check the maximum difference by rotation of arrays.

Follow the steps below to solve the problem:

- Compare the elements at odd index with even index and also keep track of them.
- Initialize two arrays
**leftDP[]**and**rightDP[]**. - For every odd index,
**leftDP[]**stores the difference of the element at current index with the element on its left and rightDP[] stores that of the right. - If the difference calculated for the previous index is positive, add it to the current difference:

if(dp[i – 1] > 0)

dp[i] = dp[i-1] + curr_diff - Otherwise, store the current difference:

dp[i] = curr_diff;

Below is the implementation of the above approach:

## Java

`// Java Program to implement ` `// the above approach ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to return maximized sum ` ` ` `// at even indices ` ` ` `public` `static` `int` `maximizeSum(` `int` `[] arr) ` ` ` `{ ` ` ` ` ` `int` `n = arr.length; ` ` ` `int` `sum = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i = i + ` `2` `) ` ` ` `sum += arr[i]; ` ` ` ` ` `// Stores difference with ` ` ` `// element on the left ` ` ` `int` `leftDP[] = ` `new` `int` `[n / ` `2` `]; ` ` ` ` ` `// Stores difference with ` ` ` `// element on the right ` ` ` `int` `rightDP[] = ` `new` `int` `[n / ` `2` `]; ` ` ` ` ` `int` `c = ` `0` `; ` ` ` ` ` `for` `(` `int` `i = ` `1` `; i < n; i = i + ` `2` `) { ` ` ` ` ` `// Compute and store ` ` ` `// left difference ` ` ` `int` `leftDiff = arr[i] ` ` ` `- arr[i - ` `1` `]; ` ` ` ` ` `// For first index ` ` ` `if` `(c - ` `1` `< ` `0` `) ` ` ` `leftDP = leftDiff; ` ` ` ` ` `else` `{ ` ` ` ` ` `// If previous difference ` ` ` `// is positive ` ` ` `if` `(leftDP > ` `0` `) ` ` ` `leftDP = leftDiff ` ` ` `+ leftDP; ` ` ` ` ` `// Otherwise ` ` ` `else` ` ` `leftDP[i] = leftDiff; ` ` ` `} ` ` ` ` ` `int` `rightDiff; ` ` ` ` ` `// For the last index ` ` ` `if` `(i + ` `1` `>= arr.length) ` ` ` `rightDiff = ` `0` `; ` ` ` ` ` `// Otherwise ` ` ` `else` ` ` `rightDiff = arr[i] ` ` ` `- arr[i + ` `1` `]; ` ` ` ` ` `// For first index ` ` ` `if` `(c - ` `1` `< ` `0` `) ` ` ` `rightDP = rightDiff; ` ` ` `else` `{ ` ` ` ` ` `// If the previous difference ` ` ` `// is positive ` ` ` `if` `(rightDP > ` `0` `) ` ` ` `rightDP = rightDiff ` ` ` `+ rightDP; ` ` ` `else` ` ` `rightDP = rightDiff; ` ` ` `} ` ` ` `c++; ` ` ` `} ` ` ` `int` `max = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < n / ` `2` `; i++) { ` ` ` `max = Math.max(max, ` ` ` `Math.max( ` ` ` `leftDP[i], ` ` ` `rightDP[i])); ` ` ` `} ` ` ` ` ` `return` `max + sum; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `arr[] = { ` `7` `, ` `8` `, ` `4` `, ` `5` `, ` `7` `, ` `6` `, ` ` ` `8` `, ` `9` `, ` `7` `, ` `3` `}; ` ` ` `int` `ans = maximizeSum(arr); ` ` ` `System.out.println(ans); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

37

**Time Complexity: **O(N)

**Auxiliary Space:** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Reverse a subarray of the given array to minimize the sum of elements at even position
- Maximize length of Subarray of 1's after removal of a pair of consecutive Array elements
- Maximize the subarray sum after multiplying all elements of any subarray with X
- Sort an array where a subarray of a sorted array is in reverse order
- Maximize sum of atmost K elements in array by taking only corner elements | Set 2
- Maximize sum of K elements in Array by taking only corner elements
- Maximize the sum of X+Y elements by picking X and Y elements from 1st and 2nd array
- Maximize elements using another array
- Maximize the median of the given array after adding K elements to the same array
- Maximize number of elements from Array with sum at most K
- Maximize the sum of sum of the Array by removing end elements
- Maximize Array sum by swapping at most K elements with another array
- Maximize the cost of reducing array elements
- Maximize array elements upto given number
- Maximize array sum by concatenating corresponding elements of given two arrays
- Maximize distance between two elements of Array by at most X swaps
- Merge the elements in subarray of all even elements of the Array
- Maximize difference between the Sum of the two halves of the Array after removal of N elements
- Maximize count of distinct elements possible in an Array from the given operation
- Reverse the elements only at odd positions in the given Array

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.