Open In App

Count of indices up to which prefix and suffix sum is equal for given Array

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
19 Likes
Like
Report

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: 2
Explanation:  The indices up to which prefix and suffix sum are equal are given below:
At index 1 prefix and suffix sum are 9
At index 2 prefix and suffix sum are 9

Input: arr = [5, 0, 4, -1, -3, 0, 2, -2, 0, 3, 2]
Output: 3
Explanation:  The prefix subarrays and suffix subarrays with equal sum are given below:
At index 1 prefix and suffix sum are 5
At index 5 prefix and suffix sum are 5
At index 8 prefix and suffix sum are 5

 

Naive Approach: The given problem can be solved by traversing the array arr from left to right and calculating prefix sum till that index then iterating the array arr from right to left and calculating the suffix sum then checking if prefix and suffix sum are equal.

C++
// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;

// Function to calculate number of
// equal prefix and suffix sums
// till the same indices
int equalSumPreSuf(int arr[], int n)
{
    int count = 0;

    // Iterate over the array
    for (int i = 0; i < n; i++) {
        int sum1 = 0, sum2 = 0;

        // Find the left subarray sum
        for (int j = 0; j < i; j++) {
            sum1 += arr[j];
        }

        // Find the right subarray sum
        for (int j = i + 1; j < n; j++) {
            sum2 += arr[j];
        }

        // Check if both are equal
        if (sum1 == sum2)
            count++;
    }

    // Return the count
    return count;
}

// Driver code
int main()
{

    // Initialize the array
    int arr[] = { 9, 0, 0, -1, 11, -1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    // Call the function and
    // print its result
    cout << (equalSumPreSuf(arr, n));
}
Java Python3 C# JavaScript

Output
2

Time Complexity: O(N^2)
Auxiliary Space: O(1)

Better Approach:

This approach to solve the problem is to precompute the prefix and suffix sum and store the result for both in different arrays. Now count the number of indices where prefix[i] == suffix[i]. This count will be our answer. 

Algorithm:

  1. Initialize an array arr of n integers.
  2. Calculate the prefix sum of the array and store it in a vector prefix. The prefix sum of an element at index i is the sum of all the elements from index 0 to i.
  3. Calculate the suffix sum of the array and store it in a vector suffix. The suffix sum of an element at index i is the sum of all the elements from index i to n-1.
  4. Initialize a variable count to 0.
  5. Traverse the array and check if prefix[i] is equal to suffix[i]. If it is, then increment count.
  6. Return the count as the answer.

Below is the implementation of the approach:

C++
// C++ code for the above approach

#include <bits/stdc++.h>
using namespace std;

// Function to calculate number of
// equal prefix and suffix sums
// till the same indices
int equalSumPreSuf(int arr[], int n) {
    // Calculate prefix sum
    vector<int> prefix(n);
    prefix[0] = arr[0];
    for (int i = 1; i < n; i++) {
        prefix[i] = prefix[i - 1] + arr[i];
    }

    // Calculate suffix sum
    vector<int> suffix(n);
    suffix[n - 1] = arr[n - 1];
    for (int i = n - 2; i >= 0; i--) {
        suffix[i] = suffix[i + 1] + arr[i];
    }

    // Count the number of indices where prefix[i] == suffix[i]
    int count = 0;
    for (int i = 0; i < n; i++) {
        if (prefix[i] == suffix[i]) {
            count++;
        }
    }

    return count;
}

int main() {
    // Initialize the array
    int arr[] = {5, 0, 4, -1, -3, 0,
                 2, -2, 0, 3, 2};
    int n = sizeof(arr) / sizeof(arr[0]);
  
    // Call the function and
    // print its result
    cout << (equalSumPreSuf(arr, n));

    return 0;
}
Java Python3 C# JavaScript

Output
3

Time Complexity: O(N) where N is size of input array. This is because a for loop runs from 1 to N.

Space Complexity: O(N) as vectors prefix and suffix are created of size N where N is size of input array.

Approach: The above approach can be optimized by iterating the array arr twice. The idea is to precompute the suffix sum as the total subarray sum. Then iterate the array a second time to calculate prefix sum at every index then comparing the prefix and suffix sums and update the suffix sum. Follow the steps below to solve the problem:

  • Initialize a variable res to zero to calculate the answer
  • Initialize a variable sufSum to store the suffix sum
  • Initialize a variable preSum to store the prefix sum
  • Traverse the array arr and add every element arr[i] to sufSum
  • Iterate the array arr again at every iteration:
    • Add the current element arr[i] into preSum
    • If preSum and sufSum are equal then increment the value of res by 1
    • Subtract the current element arr[i] from sufSum
  • Return the answer stored in res

Below is the implementation of the above approach:

C++
// C++ code for the above approach
#include <bits/stdc++.h>
using namespace std;

// Function to calculate number of
// equal prefix and suffix sums
// till the same indices
int equalSumPreSuf(int arr[], int n)
{

    // Initialize a variable
    // to store the result
    int res = 0;

    // Initialize variables to
    // calculate prefix and suffix sums
    int preSum = 0, sufSum = 0;

    // Length of array arr
    int len = n;

    // Traverse the array from right to left
    for (int i = len - 1; i >= 0; i--)
    {

        // Add the current element
        // into sufSum
        sufSum += arr[i];
    }

    // Iterate the array from left to right
    for (int i = 0; i < len; i++)
    {

        // Add the current element
        // into preSum
        preSum += arr[i];

        // If prefix sum is equal to
        // suffix sum then increment res by 1
        if (preSum == sufSum)
        {

            // Increment the result
            res++;
        }

        // Subtract the value of current
        // element arr[i] from suffix sum
        sufSum -= arr[i];
    }

    // Return the answer
    return res;
}

// Driver code
int main()
{

    // Initialize the array
    int arr[] = {5, 0, 4, -1, -3, 0,
                 2, -2, 0, 3, 2};
    int n = sizeof(arr) / sizeof(arr[0]);
    // Call the function and
    // print its result
    cout << (equalSumPreSuf(arr, n));
}

// This code is contributed by Potta Lokesh
Java Python3 C# JavaScript

Output
3

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


Explore