# Minimum value to add to arr[i] so that an array can be split at index i with equal sum

Given an array arr[] of integers, the task is to find the minimum non-negative integer k such that there exists an index jin the given array such that when arr[j] is updated as arr[j] + k, the sum of elements of array from index arr to arr[j] is equal to the sum of elements from arr[j + 1] to arr[n – 1] i.e.

arr + arr + … + arr[j] = arr[j + 1] + arr[j + 2] + … + arr[n – 1]

.
If no such k exists then print -1.

Examples:

Input: arr[] = {6, 7, 1, 3, 8, 2, 4}
Output: 3
If 3 is added to 1 sum of elements from index 0 to 2 and 3 to 6 will be equal to 17.

Input: arr[] = {7, 3}
Output: -1

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

A simple approach is to run two loops. For every element, find the difference between sums of elements on left and right. Finally, return minimum difference between the two sums.

An efficient approach: is to first calculate the prefix sum and store in an array pre[] where pre[i] stores the sum of array elements from arr to arr[i]. For each index, if the sum of elements left to it (including the element itself i.e. pre[i]) is less than or equal to the sum of right elements (pre[n – 1] – pre[i]) then update the value of k as min(k, (pre[n – 1] – pre[i]) – pre[i])

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the minimum value k to be added ` `int` `FindMinNum(``int` `arr[], ``int` `n) ` `{ ` ` `  `    ``// Array to store prefix sum ` `    ``int` `pre[n]; ` ` `  `    ``// Initialize the prefix value for first index ` `    ``// as the first element of the array ` `    ``pre = arr; ` ` `  `    ``// Compute the prefix sum for rest of the indices ` `    ``for` `(``int` `i = 1; i < n; i++) ` `        ``pre[i] = pre[i - 1] + arr[i]; ` ` `  `    ``int` `k = INT_MAX; ` ` `  `    ``for` `(``int` `i = 0; i < n - 1; i++) { ` ` `  `        ``// Sum of elements from arr[i + 1] to arr[n - 1] ` `        ``int` `rightSum = pre[n - 1] - pre[i]; ` ` `  `        ``// If sum on the right side of the ith element ` `        ``// is greater than or equal to the sum on the ` `        ``// left side then update the value of k ` `        ``if` `(rightSum >= pre[i]) ` `            ``k = min(k, rightSum - pre[i]); ` `    ``} ` ` `  `    ``if` `(k != INT_MAX) ` `        ``return` `k; ` ` `  `    ``return` `-1; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 6, 7, 1, 3, 8, 2, 4 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``cout << FindMinNum(arr, n); ` `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach  ` `class` `GfG  ` `{  ` ` `  `// Function to return the minimum value k to be added  ` `static` `int` `FindMinNum(``int` `arr[], ``int` `n)  ` `{  ` ` `  `    ``// Array to store prefix sum  ` `    ``int` `pre[] = ``new` `int``[n];  ` ` `  `    ``// Initialize the prefix value for first index  ` `    ``// as the first element of the array  ` `    ``pre[``0``] = arr[``0``];  ` ` `  `    ``// Compute the prefix sum for rest of the indices  ` `    ``for` `(``int` `i = ``1``; i < n; i++)  ` `        ``pre[i] = pre[i - ``1``] + arr[i];  ` ` `  `    ``int` `k = Integer.MAX_VALUE;  ` ` `  `    ``for` `(``int` `i = ``0``; i < n - ``1``; i++) ` `    ``{  ` ` `  `        ``// Sum of elements from arr[i + 1] to arr[n - 1]  ` `        ``int` `rightSum = pre[n - ``1``] - pre[i];  ` ` `  `        ``// If sum on the right side of the ith element  ` `        ``// is greater than or equal to the sum on the  ` `        ``// left side then update the value of k  ` `        ``if` `(rightSum >= pre[i])  ` `            ``k = Math.min(k, rightSum - pre[i]);  ` `    ``}  ` ` `  `    ``if` `(k != Integer.MAX_VALUE)  ` `        ``return` `k;  ` ` `  `    ``return` `-``1``;  ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `main(String[] args)  ` `{  ` `    ``int` `arr[] = { ``6``, ``7``, ``1``, ``3``, ``8``, ``2``, ``4` `};  ` `    ``int` `n = arr.length;  ` `    ``System.out.println(FindMinNum(arr, n));  ` `} ` `}  ` ` `  `// This code is contributed by Prerna Saini `

## Python3

 `# Python 3 implementation of the approach ` `import` `sys ` ` `  `# Function to return the minimum  ` `# value k to be added ` `def` `FindMinNum(arr, n): ` `     `  `    ``# Array to store prefix sum ` `    ``pre ``=` `[``0` `for` `i ``in` `range``(n)] ` ` `  `    ``# Initialize the prefix value for first  ` `    ``# index as the first element of the array ` `    ``pre[``0``] ``=` `arr[``0``] ` ` `  `    ``# Compute the prefix sum for rest  ` `    ``# of the indices ` `    ``for` `i ``in` `range``(``1``, n, ``1``): ` `        ``pre[i] ``=` `pre[i ``-` `1``] ``+` `arr[i] ` ` `  `    ``k ``=` `sys.maxsize ` ` `  `    ``for` `i ``in` `range``(n ``-` `1``): ` `         `  `        ``# Sum of elements from arr[i + 1] to arr[n - 1] ` `        ``rightSum ``=` `pre[n ``-` `1``] ``-` `pre[i] ` ` `  `        ``# If sum on the right side of the ith element ` `        ``# is greater than or equal to the sum on the ` `        ``# left side then update the value of k ` `        ``if` `(rightSum >``=` `pre[i]): ` `            ``k ``=` `min``(k, rightSum ``-` `pre[i]) ` ` `  `    ``if` `(k !``=` `sys.maxsize): ` `        ``return` `k ` ` `  `    ``return` `-``1` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``arr ``=` `[``6``, ``7``, ``1``, ``3``, ``8``, ``2``, ``4``] ` `    ``n ``=` `len``(arr) ` `    ``print``(FindMinNum(arr, n)) ` ` `  `# This code is contributed by ` `# Surendra_Gangwar `

## C#

 `// C# implementation of the approach  ` `using` `System; ` ` `  `class` `GfG  ` `{  ` ` `  `    ``// Function to return the minimum value k to be added  ` `    ``static` `int` `FindMinNum(``int` `[]arr, ``int` `n)  ` `    ``{  ` `     `  `        ``// Array to store prefix sum  ` `        ``int` `[]pre = ``new` `int``[n];  ` `     `  `        ``// Initialize the prefix value for first index  ` `        ``// as the first element of the array  ` `        ``pre = arr;  ` `     `  `        ``// Compute the prefix sum for rest of the indices  ` `        ``for` `(``int` `i = 1; i < n; i++)  ` `            ``pre[i] = pre[i - 1] + arr[i];  ` `     `  `        ``int` `k = ``int``.MaxValue;  ` `     `  `        ``for` `(``int` `i = 0; i < n - 1; i++) ` `        ``{  ` `     `  `            ``// Sum of elements from arr[i + 1] to arr[n - 1]  ` `            ``int` `rightSum = pre[n - 1] - pre[i];  ` `     `  `            ``// If sum on the right side of the ith element  ` `            ``// is greater than or equal to the sum on the  ` `            ``// left side then update the value of k  ` `            ``if` `(rightSum >= pre[i])  ` `                ``k = Math.Min(k, rightSum - pre[i]);  ` `        ``}  ` `     `  `        ``if` `(k != ``int``.MaxValue)  ` `            ``return` `k;  ` `     `  `        ``return` `-1;  ` `    ``}  ` `     `  `    ``// Driver code  ` `    ``public` `static` `void` `Main()  ` `    ``{  ` `        ``int` `[]arr = { 6, 7, 1, 3, 8, 2, 4 };  ` `        ``int` `n = arr.Length;  ` `         `  `        ``Console.WriteLine(FindMinNum(arr, n));  ` `    ``} ` `}  ` ` `  `// This code is contributed by Ryuga `

## PHP

 `= ``\$pre``[``\$i``]) ` `            ``\$k` `= min(``\$k``, ``\$rightSum` `- ``\$pre``[``\$i``]); ` `    ``} ` ` `  `    ``if` `(``\$k` `!= PHP_INT_MAX) ` `        ``return` `\$k``; ` ` `  `    ``return` `-1; ` `} ` ` `  `// Driver code ` `\$arr` `= ``array``(6, 7, 1, 3, 8, 2, 4); ` `\$n` `= sizeof(``\$arr``); ` `echo` `FindMinNum(``\$arr``, ``\$n``); ` ` `  `// This code is contributed by Akanksha Rai ` `?> `

Output:

```3
```

Time Complexity : O(n)
Auxiliary Space : O(n)

Further optimization : We can avoid use of extra space using below steps.
1) Compute sum of all elements.
2) Keep adding left sum and right sum can be obtained by subtracting left sum from the total sum.
The idea is similar to optimized solution of equilibrium index problem.

Time Complexity : O(n)
Auxiliary Space : O(1)

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.

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.