# Maximum sum of the array after dividing it into three segments

Given an array a of size N. The task is to find the maximum sum of array possible by dividing the array into three segments such that each element in the first segment is multiplied by -1 and each element in the second segment is multiplied by 1 and each element in the third segment is multiplied by -1. The segments can intersect and any segment may include zero in it.

Examples:

Input : a[] = {-6, 10, -3, 10, -2}
Output : 25
Divide the segments as {-6}, {10, -3, 10}, {-2)

Input : a[] = {-6, -10}
Output : 16

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:
First we need is to calculate for all possible situations for every ith element where the division should be made.

• In the first traversal find if the ith element produces maximum sum by multiplying with -1 or keeping it as it is.
• Store all values in the array b.
• In the second traversal find maximum sum by decreasing a[i] and adding b[i] to it.

Below is the implementation of the above approach :

## C++

 `// C++ program to find maximum sum of array  ` `// after dividing it into three segments ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find maximum sum of array  ` `// after dividing it into three segments ` `int` `Max_Sum(``int` `a[], ``int` `n) ` `{ ` `    ``// To store sum upto ith index ` `    ``int` `b[n]; ` `    ``int` `S = 0; ` `    ``int` `res = 0; ` `     `  `    ``// Traverse through the array ` `    ``for` `(``int` `i = 0; i < n; i++) ` `    ``{ ` `        ``b[i] = res; ` `        ``res += a[i]; ` `        ``S += a[i]; ` `         `  `        ``// Get the maximum possible sum ` `        ``res = max(res, -S); ` `    ``} ` `     `  `    ``// Store in the reuired answer ` `    ``int` `ans = S; ` `     `  `    ``// Maximum sum starting from left segment ` `    ``// by choosing between keeping array element as ` `    ``// it is or subtracting it ` `    ``ans = max(ans, res); ` ` `  ` `  `    ``// Finding maximum sum by decreasing a[i] and  ` `    ``// adding b[i] to it that means max(multiplying  ` `    ``// it by -1 or using b[i] value) ` `    ``int` `g = 0; ` `     `  `    ``// For third segment ` `    ``for` `(``int` `i = n - 1; i >= 0; --i) { ` `        ``g -= a[i]; ` `        ``ans = max(ans, g + b[i]); ` `    ``} ` `     `  `    ``// return the required answer ` `    ``return` `ans; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `a[] = { -6, 10, -3, 10, -2 }; ` ` `  `    ``int` `n = ``sizeof``(a) / ``sizeof``(a); ` `     `  `    ``// Function call ` `    ``cout << ``"Maximum sum is: "` `<< Max_Sum(a, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to find maximum sum of array  ` `// after dividing it into three segments ` `import` `java.util.*; ` ` `  `class` `GFG  ` `{ ` ` `  `// Function to find maximum sum of array  ` `// after dividing it into three segments ` `static` `int` `Max_Sum(``int` `a[], ``int` `n) ` `{ ` `    ``// To store sum upto ith index ` `    ``int` `[]b = ``new` `int``[n]; ` `    ``int` `S = ``0``; ` `    ``int` `res = ``0``; ` `     `  `    ``// Traverse through the array ` `    ``for` `(``int` `i = ``0``; i < n; i++) ` `    ``{ ` `        ``b[i] = res; ` `        ``res += a[i]; ` `        ``S += a[i]; ` `         `  `        ``// Get the maximum possible sum ` `        ``res = Math.max(res, -S); ` `    ``} ` `     `  `    ``// Store in the reuired answer ` `    ``int` `ans = S; ` `     `  `    ``// Maximum sum starting from left segment ` `    ``// by choosing between keeping array element as ` `    ``// it is or subtracting it ` `    ``ans = Math.max(ans, res); ` ` `  `    ``// Finding maximum sum by decreasing a[i] and  ` `    ``// adding b[i] to it that means max(multiplying  ` `    ``// it by -1 or using b[i] value) ` `    ``int` `g = ``0``; ` `     `  `    ``// For third segment ` `    ``for` `(``int` `i = n - ``1``; i >= ``0``; --i)  ` `    ``{ ` `        ``g -= a[i]; ` `        ``ans = Math.max(ans, g + b[i]); ` `    ``} ` `     `  `    ``// return the required answer ` `    ``return` `ans; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args)  ` `{ ` `    ``int` `a[] = { -``6``, ``10``, -``3``, ``10``, -``2` `}; ` ` `  `    ``int` `n = a.length; ` `     `  `    ``// Function call ` `    ``System.out.println(``"Maximum sum is: "` `+  ` `                            ``Max_Sum(a, n)); ` `} ` `} ` ` `  `// This code is contributed by Princi Singh `

## Python3

 `# Python3 program to find  ` `# maximum sum of array after  ` `# dividing it into three segments ` ` `  `# Function to find maximum sum of array ` `# after dividing it into three segments ` `def` `Max_Sum(a, n): ` `     `  `    ``# To store sum upto ith index ` `    ``b ``=` `[``0` `for` `i ``in` `range``(n)] ` `    ``S ``=` `0` `    ``res ``=` `0` ` `  `    ``# Traverse through the array ` `    ``for` `i ``in` `range``(n): ` `        ``b[i] ``=` `res ` `        ``res ``+``=` `a[i] ` `        ``S ``+``=` `a[i] ` ` `  `        ``# Get the maximum possible sum ` `        ``res ``=` `max``(res, ``-``S) ` ` `  `    ``# Store in the reuired answer ` `    ``ans ``=` `S ` ` `  `    ``# Maximum sum starting from  ` `    ``# left segment by choosing between ` `    ``# keeping array element as it is ` `    ``# or subtracting it ` `    ``ans ``=` `max``(ans, res) ` ` `  `    ``# Finding maximum sum by decreasing  ` `    ``# a[i] and adding b[i] to it  ` `    ``# that means max(multiplying it  ` `    ``# by -1 or using b[i] value) ` `    ``g ``=` `0` ` `  `    ``# For third segment ` `    ``for` `i ``in` `range``(n ``-` `1``, ``-``1``, ``-``1``): ` `        ``g ``-``=` `a[i] ` `        ``ans ``=` `max``(ans, g ``+` `b[i]) ` ` `  `    ``# return the required answer ` `    ``return` `ans ` ` `  `# Driver code ` `a ``=` `[``-``6``, ``10``, ``-``3``, ``10``, ``-``2``] ` ` `  `n ``=` `len``(a) ` ` `  `# Function call ` `print``(``"Maximum sum is:"``,  ` `          ``Max_Sum(a, n)) ` `            `  `# This code is contributed ` `# by Mohit Kumar `

## C#

 `// C#+ program to find maximum sum of array  ` `// after dividing it into three segments ` `using` `System; ` ` `  `class` `GFG  ` `{ ` ` `  `// Function to find maximum sum of array  ` `// after dividing it into three segments ` `static` `int` `Max_Sum(``int` `[]a, ``int` `n) ` `{ ` `    ``// To store sum upto ith index ` `    ``int` `[]b = ``new` `int``[n]; ` `    ``int` `S = 0; ` `    ``int` `res = 0; ` `     `  `    ``// Traverse through the array ` `    ``for` `(``int` `i = 0; i < n; i++) ` `    ``{ ` `        ``b[i] = res; ` `        ``res += a[i]; ` `        ``S += a[i]; ` `         `  `        ``// Get the maximum possible sum ` `        ``res = Math.Max(res, -S); ` `    ``} ` `     `  `    ``// Store in the reuired answer ` `    ``int` `ans = S; ` `     `  `    ``// Maximum sum starting from left segment ` `    ``// by choosing between keeping array element  ` `    ``// as it is or subtracting it ` `    ``ans = Math.Max(ans, res); ` ` `  `    ``// Finding maximum sum by decreasing a[i] and  ` `    ``// adding b[i] to it that means max(multiplying  ` `    ``// it by -1 or using b[i] value) ` `    ``int` `g = 0; ` `     `  `    ``// For third segment ` `    ``for` `(``int` `i = n - 1; i >= 0; --i)  ` `    ``{ ` `        ``g -= a[i]; ` `        ``ans = Math.Max(ans, g + b[i]); ` `    ``} ` `     `  `    ``// return the required answer ` `    ``return` `ans; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main()  ` `{ ` `    ``int` `[]a = { -6, 10, -3, 10, -2 }; ` ` `  `    ``int` `n = a.Length; ` `     `  `    ``// Function call ` `    ``Console.WriteLine(``"Maximum sum is: "` `+  ` `                           ``Max_Sum(a, n)); ` `} ` `} ` ` `  `// This code is contributed by anuj_67.. `

## PHP

 `= 0; --``\$i``) ` `    ``{  ` `        ``\$g` `-= ``\$a``[``\$i``];  ` `        ``\$ans` `= max(``\$ans``, ``\$g` `+ ``\$b``[``\$i``]);  ` `    ``}  ` `     `  `    ``// return the required answer  ` `    ``return` `\$ans``;  ` `}  ` ` `  `// Driver code  ` `\$a` `= ``array``(-6, 10, -3, 10, -2 );  ` ` `  `\$n` `= ``count``(``\$a``); ` ` `  `// Function call  ` `echo` `(``"Maximum sum is: "``); ` `echo` `Max_Sum(``\$a``, ``\$n``); ` ` `  `// This code is contributed by Naman_garg.  ` `?>  `

Output:

```Maximum sum is: 25
```

Time complexity : O(N)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.