Index with Minimum sum of prefix and suffix sums in an Array
Last Updated :
21 Aug, 2023
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 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 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.
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++
#include <bits/stdc++.h>
using namespace std;
int indexMinSum( int arr[], int n)
{
int min = arr[0];
int index = 0;
for ( int i = 1; i < n; i++) {
if (arr[i] < min) {
min = arr[i];
index = i;
}
}
return index + 1;
}
int main()
{
int arr[] = { 6, 8, 2, 3, 5 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << indexMinSum(arr, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int indexMinSum( int arr[], int n)
{
int min = arr[ 0 ];
int index = 0 ;
for ( int i = 1 ; i < n; i++) {
if (arr[i] < min) {
min = arr[i];
index = i;
}
}
return index + 1 ;
}
public static void main (String[] args) {
int arr[] = { 6 , 8 , 2 , 3 , 5 };
int n =arr.length;
System.out.println( indexMinSum(arr, n));
}
}
|
Python 3
def indexMinSum(arr, n):
min = arr[ 0 ]
index = 0
for i in range ( 1 , n) :
if (arr[i] < min ) :
min = arr[i]
index = i
return index + 1
if __name__ = = "__main__" :
arr = [ 6 , 8 , 2 , 3 , 5 ]
n = len (arr)
print (indexMinSum(arr, n))
|
C#
using System;
class GFG
{
static int indexMinSum( int []arr, int n)
{
int min = arr[0];
int index = 0;
for ( int i = 1; i < n; i++) {
if (arr[i] < min) {
min = arr[i];
index = i;
}
}
return index + 1;
}
static void Main()
{
int []arr = { 6, 8, 2, 3, 5 };
int n =arr.Length;
Console.WriteLine(indexMinSum(arr, n));
}
}
|
Javascript
<script>
function indexMinSum(arr,n)
{
let min = arr[0];
let index = 0;
for (let i = 1; i < n; i++) {
if (arr[i] < min) {
min = arr[i];
index = i;
}
}
return index + 1;
}
let arr=[6, 8, 2, 3, 5];
let n =arr.length;
document.write( indexMinSum(arr, n));
</script>
|
PHP
<?php
function indexMinSum( $arr , $n )
{
$min = $arr [0];
$index = 0;
for ( $i = 1; $i < $n ; $i ++)
{
if ( $arr [ $i ] < $min )
{
$min = $arr [ $i ];
$index = $i ;
}
}
return ( $index + 1);
}
$arr = array (6, 8, 2, 3, 5 );
$n = sizeof( $arr );
echo indexMinSum( $arr , $n );
?>
|
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++
#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;
}
int main()
{
int arr[] = { 6, 8, 2, 3, 5 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << indexMinSum(arr, n);
return 0;
}
|
Java
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 ;
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;
}
public static void main(String[] args)
{
int [] arr = { 6 , 8 , 2 , 3 , 5 };
int n = arr.length;
System.out.println(indexMinSum(arr, n));
}
}
|
Python3
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
if __name__ = = "__main__" :
arr = [ 6 , 8 , 2 , 3 , 5 ]
n = len (arr)
print (indexMinSum(arr, n))
|
C#
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;
}
public static void Main()
{
int [] arr = { 6, 8, 2, 3, 5 };
int n = arr.Length;
Console.WriteLine(IndexMinSum(arr, n));
}
}
|
Javascript
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;
}
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...