# Index with Minimum sum of prefix and suffix sums in an Array

Given an Array of integers. The task is to find the index in the array at which the value of prefixSum(i) + suffixSum(i) is minimum.

Note:

• PrefixSum(i) = The sum of first i numbers of the array.
• SuffixSum(i) = the sum of last N – i + 1 numbers of the array.
• 1-based indexing is considered for the array. That is index of the first element in array is 1. Examples:

```Input : arr[] = {3, 5, 1, 6, 6 }
Output : 3
Explanation:
Presum[] = {3, 8, 9, 15, 21}
Postsum[] = { 21, 18, 13, 12, 6}
Presum[] + Postsum[] = {24, 26, 22, 27, 27}
It is clear that the min value of sum of
prefix and suffix sum is 22 at index 3.

Input : arr[] = { 3, 2, 5, 7, 3 }
Output : 2
```

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

Given that we need to minimize the value of PrefixSum[i] + SuffixSum[i]. That is sum of first elements and elements from end.

If observed carefully, it can be seen that:

PrefixSum[i] + SuffixSum[i] = Sum of all elements in array + arr[i](Element at i-th index)

Since sum of all elements of the array will be the same for every index, therefore the value of PrefixSum[i] + SuffixSum[i] will be minimum for the minimum value of arr[i].

Therefore, the task reduces to find only the index of the minimum element of the array.

Below is the implementation of the above approach:

## C++

 `// C++ program to find the index with ` `// minimum sum of prefix and suffix ` `// sums in an Array ` ` `  `#include ` `using` `namespace` `std; ` ` `  `int` `indexMinSum(``int` `arr[], ``int` `n) ` `{ ` `    ``// Initialization of the min value ` `    ``int` `min = arr; ` `    ``int` `index = 0; ` ` `  `    ``// Find minimum element in the array ` `    ``for` `(``int` `i = 1; i < n; i++) { ` `        ``if` `(arr[i] < min) { ` ` `  `            ``// store the index of the ` `            ``// current minimum element ` `            ``min = arr[i]; ` `            ``index = i; ` `        ``} ` `    ``} ` ` `  `    ``// return the index of min element ` `    ``// 1-based index ` `    ``return` `index + 1; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 6, 8, 2, 3, 5 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``cout << indexMinSum(arr, n); ` `    ``return` `0; ` `} `

## Java

 `// Java program to find the index with ` `// minimum sum of prefix and suffix ` `// sums in an Array ` ` `  `import` `java.io.*; ` ` `  `class` `GFG { ` ` `  `static` `int` `indexMinSum(``int` `arr[], ``int` `n) ` `{ ` `    ``// Initialization of the min value ` `    ``int` `min = arr[``0``]; ` `    ``int` `index = ``0``; ` ` `  `    ``// Find minimum element in the array ` `    ``for` `(``int` `i = ``1``; i < n; i++) { ` `        ``if` `(arr[i] < min) { ` ` `  `            ``// store the index of the ` `            ``// current minimum element ` `            ``min = arr[i]; ` `            ``index = i; ` `        ``} ` `    ``} ` ` `  `    ``// return the index of min element ` `    ``// 1-based index ` `    ``return` `index + ``1``; ` `} ` ` `  `// Driver Code ` `    ``public` `static` `void` `main (String[] args) { ` `    ``int` `arr[] = { ``6``, ``8``, ``2``, ``3``, ``5` `}; ` `    ``int` `n =arr.length; ` `    ``System.out.println( indexMinSum(arr, n)); ` `    ``} ` `} ` `// This code is contributed by inder_verma.. `

## Python 3

 `# Python 3 program to find the index with ` `# minimum sum of prefix and suffix ` `# sums in an Array ` ` `  `def` `indexMinSum(arr, n): ` ` `  `    ``# Initialization of the min value ` `    ``min` `=` `arr[``0``] ` `    ``index ``=` `0` ` `  `    ``# Find minimum element in the array ` `    ``for` `i ``in` `range``(``1``, n) : ` `        ``if` `(arr[i] < ``min``) : ` ` `  `            ``# store the index of the ` `            ``# current minimum element ` `            ``min` `=` `arr[i] ` `            ``index ``=` `i ` ` `  `    ``# return the index of min element ` `    ``# 1-based index ` `    ``return` `index ``+` `1` ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"``: ` `     `  `    ``arr ``=` `[ ``6``, ``8``, ``2``, ``3``, ``5` `] ` `    ``n ``=` `len``(arr) ` `    ``print``(indexMinSum(arr, n)) ` ` `  `# This code is contributed by ita_c `

## C#

 `// C# program to find the index with ` `// minimum sum of prefix and suffix ` `// sums in an Array ` ` `  `using` `System; ` `class` `GFG ` `{ ` `    ``static` `int` `indexMinSum(``int` `[]arr, ``int` `n) ` `    ``{ ` `        ``// Initialization of the min value ` `        ``int` `min = arr; ` `        ``int` `index = 0; ` `     `  `        ``// Find minimum element in the array ` `        ``for` `(``int` `i = 1; i < n; i++) { ` `            ``if` `(arr[i] < min) { ` `     `  `                ``// store the index of the ` `                ``// current minimum element ` `                ``min = arr[i]; ` `                ``index = i; ` `            ``} ` `        ``} ` `     `  `        ``// return the index of min element ` `        ``// 1-based index ` `        ``return` `index + 1; ` `    ``} ` `     `  `     `  `    ``// Driver Code ` `    ``static` `void` `Main() ` `    ``{ ` `        ``int` `[]arr = { 6, 8, 2, 3, 5 }; ` `        ``int` `n =arr.Length; ` `        ``Console.WriteLine(indexMinSum(arr, n)); ` `    ``} ` `    ``// This code is contributed by ANKITRAI1 ` `} `

## PHP

 ` `

Output:

```3
```

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.