Index with Minimum sum of prefix and suffix sums in an Array
Last Updated : 05 May, 2025
Given an array of integers. The task is to find the index [Tex]i [/Tex]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 an index of the first element in the 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
Given that we need to minimize the value of PrefixSum[i] + SuffixSum[i]. That is sum of first [Tex]i [/Tex]elements and [Tex]N-i+1 [/Tex]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.
Approach:
- Create a function “indexMinSum” that takes an integer array arr and an integer n as input.
- Initialize the “min” variable to the first element of arr and the index variable to 0.
- Start a for loop and traverse through the array from index 1 to n-1:
- If the current element is less than min, set min to the current element and index to the current index.
- Return index + 1 as the 1-based index of the minimum element.
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 <bits/stdc++.h> using namespace std; 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 int main() { int arr[] = { 6, 8, 2, 3, 5 }; int n = sizeof(arr) / sizeof(arr[0]); 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 # 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[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 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 }
JavaScript <script> // JavaScript program to find the index with // minimum sum of prefix and suffix // sums in an Array function indexMinSum(arr,n) { // Initialization of the min value let min = arr[0]; let index = 0; // Find minimum element in the array for (let 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 let arr=[6, 8, 2, 3, 5]; let n =arr.length; document.write( indexMinSum(arr, n)); // This code is contributed by avanitrachhadiya2155 </script>
PHP <?php // PHP program to find the index with // minimum sum of prefix and suffix // sums in an Array function indexMinSum($arr, $n) { // Initialization of the // min value $min = $arr[0]; $index = 0; // Find minimum element in // the array for ($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 $arr = array(6, 8, 2, 3, 5 ); $n = sizeof($arr); echo indexMinSum($arr, $n); // This code is contributed by Sachin ?>
Time Complexity: O(N)
Auxiliary Space: O(1)
Brute Force Approach:
The brute force approach to solve this problem is to iterate over each index i of the array and calculate the prefixSum(i) and suffixSum(i). Then, calculate the sum of these two values and keep track of the minimum value and its index. Finally, return the index with the minimum sum.
Here are the steps to implement this approach:
- Initialize minIndex variable to 1 and minSum variable to the maximum value that an integer can store.
- Iterate over the array from index 1 to n.
- For each index i, initialize prefixSum and suffixSum variables to 0.
- Iterate over the array from index 1 to i and compute prefixSum as the sum of elements from index 1 to i.
- Iterate over the array from index n to i and compute suffixSum as the sum of elements from index n to i.
- Calculate the sum of prefixSum and suffixSum and store it in a variable named sum.
- If the value of sum is less than minSum, update minSum to sum and minIndex to i.
- Finally, return the value of minIndex.
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 <bits/stdc++.h> using namespace std; int indexMinSum(int arr[], int n) { int minIndex = 1; int minSum = INT_MAX; for (int i = 1; i <= n; i++) { int prefixSum = 0; int suffixSum = 0; for (int j = 1; j <= i; j++) { prefixSum += arr[j-1]; } for (int j = n; j >= i; j--) { suffixSum += arr[j-1]; } int sum = prefixSum + suffixSum; if (sum < minSum) { minSum = sum; minIndex = i; } } return minIndex; } // Driver Code int main() { int arr[] = { 6, 8, 2, 3, 5 }; int n = sizeof(arr) / sizeof(arr[0]); cout << indexMinSum(arr, n); return 0; }
Java /*package whatever //do not write package name here */ // Java program to find the index with // minimum sum of prefix and suffix // sums in an Array import java.util.*; class Main { public static int indexMinSum(int[] arr, int n) { int minIndex = 1; int minSum = Integer.MAX_VALUE; for (int i = 1; i <= n; i++) { int prefixSum = 0; int suffixSum = 0; // Computing prefix sum for (int j = 1; j <= i; j++) { prefixSum += arr[j - 1]; } // Computing suffix sum for (int j = n; j >= i; j--) { suffixSum += arr[j - 1]; } int sum = prefixSum + suffixSum; if (sum < minSum) { minSum = sum; minIndex = i; } } return minIndex; } // 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)); } }
Python # Python code def indexMinSum(arr, n): minIndex = 1 minSum = float('inf') for i in range(1, n + 1): prefixSum = 0 suffixSum = 0 for j in range(1, i + 1): prefixSum += arr[j - 1] for j in range(n, i - 1, -1): suffixSum += arr[j - 1] sum = prefixSum + suffixSum if sum < minSum: minSum = sum minIndex = i return minIndex # Driver Code if __name__ == "__main__": arr = [6, 8, 2, 3, 5] n = len(arr) print(indexMinSum(arr, n))
C# // C# program to find the index with // minimum sum of prefix and suffix // sums in an Array using System; public class GFG { public static int IndexMinSum(int[] arr, int n) { int minIndex = 1; int minSum = int.MaxValue; for (int i = 1; i <= n; i++) { int prefixSum = 0; int suffixSum = 0; for (int j = 1; j <= i; j++) { prefixSum += arr[j - 1]; } for (int j = n; j >= i; j--) { suffixSum += arr[j - 1]; } int sum = prefixSum + suffixSum; if (sum < minSum) { minSum = sum; minIndex = i; } } return minIndex; } // Driver Code public static void Main() { int[] arr = { 6, 8, 2, 3, 5 }; int n = arr.Length; Console.WriteLine(IndexMinSum(arr, n)); } }
JavaScript // Javascript program to find the index with // minimum sum of prefix and suffix // sums in an Array function indexMinSum(arr) { let n = arr.length; let minIndex = 1; let minSum = Number.MAX_VALUE; for (let i = 1; i <= n; i++) { let prefixSum = 0; let suffixSum = 0; for (let j = 1; j <= i; j++) { prefixSum += arr[j - 1]; } for (let j = n; j >= i; j--) { suffixSum += arr[j - 1]; } let sum = prefixSum + suffixSum; if (sum < minSum) { minSum = sum; minIndex = i; } } return minIndex; } // Driver Code let arr = [6, 8, 2, 3, 5]; console.log(indexMinSum(arr));
Time Complexity: O(n^2) because of the nested loops used to compute the prefixSum and suffixSum arrays.
Auxiliary Space: O(1) as we are not using any extra space.
Similar Reads
Count of indices up to which prefix and suffix sum is equal for given Array
Given an array arr[] of integers, the task is to find the number of indices up to which prefix sum and suffix sum are equal. Example: Input: arr = [9, 0, 0, -1, 11, -1]Output: 2Explanation: The indices up to which prefix and suffix sum are equal are given below:At index 1 prefix and suffix sum are 9
15+ min read
Find index with minimum difference between prefix and suffix average of given Array
Given an array arr[] of size N, the task is to find an index i such that the difference between prefix average and suffix average is minimum, i.e. average of elements till i (in range [0, i]) and the average of elements after i (in range [i+1, N)) is minimum. Examples: Input: arr[] = {2, 9, 3, 5}Out
8 min read
Maximize the sum of array after multiplying a prefix and suffix by -1
Given an array arr[] of length N, the task is to maximize the sum of all the elements of the array by performing the following operations at most once. Choose a prefix of the array and multiply all the elements by -1.Choose a suffix of the array and multiply all the elements by -1. Examples: Input:
7 min read
Maximize deletions by removing prefix and suffix of Array with same sum
Given an array Arr[] of size N, the cost of removing ith element is Arr[i]. The task is to remove the maximum number of elements by removing the prefix and the suffix of the same length and having the same total cost. Examples: Input: Arr[] = {80, 90, 81, 80}Output: 2 Explanation: If we choose 80 fr
10 min read
Maximum array sum with prefix and suffix multiplications with -1 allowed
Given N elements (both positive and negative). Find the maximum sum, provided that the first operation is to take some prefix of the sequence and multiply all numbers in this prefix by -1. The second operation is to take some suffix and multiply all numbers in it by -1. The chosen prefix and suffix
8 min read
Find position forming AP with prefix and suffix sum of Array with common difference D
Given an array, arr[] of size N and a positive integer D, the task is to find the position i of an element in arr[] such that the prefix sum, arr[i] and suffix sum is in arithmetic progression with common difference D, i.e. arr[i] - sum(arr[0, . . ., i-1]) = sum(arr[i+1, . . ., N-1]) - arr[i] = D. I
8 min read
Rearrange Array such that sum of adjacent sum divided by K is minimum
Given an integer array arr[] of length N and an integer K, the task is to rearrange the elements of arr[] in such a way that the value of the total sum of Ai + Ai + 1 divided by K is minimized for all (1 ⤠i ⤠N - 1). If there are multiple arrangements, then print any of them. Examples: Input: N = 5
6 min read
Count of possible arrays from prefix-sum and suffix-sum arrays
Given 2*N integers which are elements of a prefix and suffix array(in shuffled order) of an array of size N, the task is to find the no of possible array's of the size N which can be made from these elements Examples: Input: arr[] = {5, 2, 3, 5} Output: 2 Explanation: 1st array can be : {2, 3} Its p
15+ min read
Find the suffix factorials of a suffix sum array of the given array
Given an array arr[] consisting of N positive integers, the task is to find the suffix factorials of a suffix sum array of the given array. Examples: Input: arr[] = {1, 2, 3, 4}Output: {3628800, 362880, 5040, 24}Explanation: The suffix sum of the given array is {10, 9, 7, 4}. Therefore, suffix facto
5 min read
Find the maximum number of indices having 0 as Prefix sum
Given an array arr[]. Find the maximum number of indices having 0 as prefix sums possible by replacing the 0s in the array with any arbitrary value. Examples: Input: arr[] = {2, 0, 1, -1, 0}Output: 3Explanation: On changing the 1st 0 at arr[1] to -2 the array changes to {2, -2, 1, -1, 0} which 3 pre
13 min read