Open In App

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

Last Updated : 21 Aug, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array of integers. The task is to find the index i                 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 i                 elements and N-i+1                 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 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[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
?>

                    

Output
3



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:

  1. Initialize minIndex variable to 1 and minSum variable to the maximum value that an integer can store.
  2. Iterate over the array from index 1 to n.
  3. For each index i, initialize prefixSum and suffixSum variables to 0.
  4. Iterate over the array from index 1 to i and compute prefixSum as the sum of elements from index 1 to i.
  5. Iterate over the array from index n to i and compute suffixSum as the sum of elements from index n to i.
  6. Calculate the sum of prefixSum and suffixSum and store it in a variable named sum.
  7. If the value of sum is less than minSum, update minSum to sum and minIndex to i.
  8. 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));
    }
}

                    

Python3

# 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));

                    

Output
3



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

Similar Reads