Count pairs of indices having equal prefix and suffix sums
Given an array arr[] of length N, the task is to find the count of pairs of indices (i, j) (0-based indexing) such that prefix sum of the subarray {arr[0], … arr[i]} is equal to the suffix sum of the subarray {arr[N – 1], …, arr[j]} ( 0 ? i, j < N).
Examples:
Input: arr[] = {1, 2, 1, 1}
Output: 3
Explanation:
The 3 possible pairs of indices are as follows:
- Pair {0, 3}: Prefix Sum of subarray {arr[0]} = 1. Suffix Sum of subarray {arr[3]} = 1.
- Pair {2, 1}: Prefix Sum of subarray {arr[0], .. arr[2]} = 4. Suffix Sum of subarray {arr[3], …, arr[1]} = 4.
- Pair {3, 0}: Prefix Sum of subarray {arr[0], .. arr[3]} = 5. Suffix Sum of subarray {arr[3], …, arr[0]} = 5.
Input: arr[] = {1, 0, -1}
Output: 1
Approach: The idea is to use Hashing to solve this problem. Follow the steps below to solve the problem:
- Traverse the array arr[] and calculate prefix sum for all array indices and store their frequencies in a HashMap.
- Traverse the array in reverse and keep calculating suffix sum up to every array index.
- For every suffix sum obtained, check if it is present in the Map or not. If found to be true, increase count by 1.
- Print the count obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void countPairs( int * arr, int n)
{
unordered_map< int , int > mp1;
int sum = 0;
for ( int i = 0; i < n; i++) {
sum += arr[i];
mp1[sum] += 1;
}
sum = 0;
int ans = 0;
for ( int i = n - 1; i >= 0; i--) {
sum += arr[i];
if (mp1.find(sum) != mp1.end()) {
ans += mp1[sum];
}
}
cout << ans;
}
int main()
{
int arr[] = { 1, 2, 1, 1 };
int n = sizeof (arr)
/ sizeof (arr[0]);
countPairs(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void countPairs( int []arr, int n)
{
HashMap<Integer,Integer> mp1 = new HashMap<Integer,Integer>();
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
{
sum += arr[i];
if (mp1.containsKey(sum)){
mp1.put(sum, mp1.get(sum)+ 1 );
}
else {
mp1.put(sum, 1 );
}
}
sum = 0 ;
int ans = 0 ;
for ( int i = n - 1 ; i >= 0 ; i--)
{
sum += arr[i];
if (mp1.containsKey(sum))
{
ans += mp1.get(sum);
}
}
System.out.print(ans);
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 1 , 1 };
int n = arr.length;
countPairs(arr, n);
}
}
|
Python3
def countPairs(arr, n):
mp1 = {}
sum = 0
for i in range (n):
sum + = arr[i]
mp1[ sum ] = mp1.get( sum , 0 ) + 1
sum = 0
ans = 0
for i in range (n - 1 , - 1 , - 1 ):
sum + = arr[i]
if ( sum in mp1):
ans + = mp1[ sum ]
print (ans)
if __name__ = = '__main__' :
arr = [ 1 , 2 , 1 , 1 ]
n = len (arr)
countPairs(arr, n)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void countPairs( int [] arr, int n)
{
Dictionary< int , int > mp1 = new Dictionary< int , int >();
int sum = 0;
for ( int i = 0; i < n; i++)
{
sum += arr[i];
if (mp1.ContainsKey(sum))
{
mp1.Add(sum, mp1[sum] + 1);
}
else {
mp1.Add(sum, 1);
}
}
sum = 0;
int ans = 0;
for ( int i = n - 1; i >= 0; i--)
{
sum += arr[i];
if (mp1.ContainsKey(sum))
{
ans += mp1[sum];
}
}
Console.Write(ans);
}
static public void Main ()
{
int [] arr = { 1, 2, 1, 1 };
int n = arr.Length;
countPairs(arr, n);
}
}
|
Javascript
<script>
function countPairs(arr, n)
{
let mp1 = new Map();
let sum = 0;
for (let i = 0; i < n; i++)
{
sum += arr[i];
if (mp1.has(sum))
{
mp1.set(sum, mp1.get(sum) + 1);
}
else
{
mp1.set(sum, 1)
}
}
sum = 0;
let ans = 0;
for (let i = n - 1; i >= 0; i--)
{
sum += arr[i];
if (mp1.has(sum))
{
ans += mp1.get(sum);
}
}
document.write(ans);
}
let arr = [ 1, 2, 1, 1 ];
let n = arr.length
countPairs(arr, n);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
11 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...