Count pairs of indices having sum of indices same as the sum of elements at those indices
Last Updated :
12 Dec, 2022
Given an array arr[] consisting of N integers, the task is to find the number of pairs (i, j) whose sum of indices is the same as the sum elements at the indices.
Examples:
Input: arr[] = {0, 1, 7, 4, 3, 2}
Output: 1
Explanation: There exists only pair that satisfies the condition is {(0, 1)}.
Input: arr[] = {1, 6, 2, 4, 5, 6}
Output: 0
Naive Approach: The simple approach to solve the given problem is to generate all possible pairs of the given array and if the sum of any pairs is the same as the sum of its indices, then count this pair. After checking for all the pairs, print the total count obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void countPairs( int arr[], int N)
{
int answer = 0;
for ( int i = 0; i < N; i++) {
for ( int j = i + 1; j < N; j++) {
if (arr[i] + arr[j] == i + j) {
answer++;
}
}
}
cout << answer;
}
int main()
{
int arr[] = { 0, 1, 2, 3, 4, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
countPairs(arr, N);
return 0;
}
|
Java
import java.io.*;
public class GFG{
public static void countPairs( int arr[], int N)
{
int answer = 0 ;
for ( int i = 0 ; i < N; i++)
{
for ( int j = i + 1 ; j < N; j++)
{
if (arr[i] + arr[j] == i + j)
{
answer++;
}
}
}
System.out.println(answer);
}
public static void main(String args[])
{
int arr[] = { 0 , 1 , 2 , 3 , 4 , 5 };
int N = arr.length;
countPairs(arr, N);
}
}
|
Python3
def countPairs(arr, N):
answer = 0
for i in range (N):
for j in range (i + 1 , N):
if arr[i] + arr[j] = = i + j:
answer + = 1
print (answer)
arr = [ 0 , 1 , 2 , 3 , 4 , 5 ]
N = len (arr)
countPairs(arr, N)
|
C#
using System;
class GFG{
static void countPairs( int [] arr, int N)
{
int answer = 0;
for ( int i = 0; i < N; i++)
{
for ( int j = i + 1; j < N; j++)
{
if (arr[i] + arr[j] == i + j)
{
answer++;
}
}
}
Console.Write(answer);
}
public static void Main( string [] args)
{
int [] arr = { 0, 1, 2, 3, 4, 5 };
int N = arr.Length;
countPairs(arr, N);
}
}
|
Javascript
<script>
function countPairs(arr, N)
{
let answer = 0;
for (let i = 0; i < N; i++) {
for (let j = i + 1; j < N; j++) {
if (arr[i] + arr[j] == i + j) {
answer++;
}
}
}
document.write(answer);
}
let arr = [0, 1, 2, 3, 4, 5];
let N = arr.length;
countPairs(arr, N);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can also be optimized by using an unordered map to store the count of elements having (arr[i] – i) value in the array arr[]. Follow the steps below to solve the problem:
- Initialize the variable, say answer as 0 to store the count of pairs in the array arr[].
- Initialize an unordered map mp[] to store the frequency of an element in the array arr[] having value (arr[i] – i).
- Iterate over the range [0, N] using the variable i and perform the following steps:
- Initialize the variable keyValue as the value of (arr[i] – i).
- Increase the value of keyValue in the unordered map mp[] by 1.
- Iterate over the unordered map mp[] using the variable i and perform the following steps:
- Initialize the variable size as i.second the value of the unordered map mp[].
- Add the value of size*(size – 1)/2 to the variable answer.
- After performing the above steps, print the value of the answer as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void countPairs( int arr[], int N)
{
int answer = 0;
unordered_map< int , int > mp;
for ( int i = 0; i < N; i++) {
int keyValue = arr[i] - i;
mp[keyValue]++;
}
for ( auto i : mp) {
int size = i.second;
answer += (size * (size - 1)) / 2;
}
cout << answer;
}
int main()
{
int arr[] = { 0, 1, 2, 3, 4, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
countPairs(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static void countPairs( int [] arr, int n)
{
int answer = 0 ;
HashMap<Integer, Integer> mp
= new HashMap<Integer, Integer>();
for ( int i = 0 ; i < n; i++) {
int value = arr[i] - i;
if (mp.containsKey(value)) {
mp.put(value, mp.get(value) + 1 );
}
else {
mp.put(value, 1 );
}
}
for (Map.Entry<Integer, Integer> map :
mp.entrySet()) {
int temp = map.getValue();
answer += temp * (temp - 1 ) / 2 ;
}
System.out.println(answer);
}
public static void main(String[] args)
{
int [] arr = { 0 , 1 , 2 , 3 , 4 , 5 };
int n = 6 ;
countPairs(arr, n);
}
}
|
Python3
def countPairs(arr, N):
answer = 0
mp = {}
for i in range (N):
keyValue = arr[i] - i
if keyValue in mp.keys():
mp[keyValue] + = 1
else :
mp[keyValue] = 1
for size in mp.values():
answer + = (size * (size - 1 )) / / 2
print (answer)
arr = [ 0 , 1 , 2 , 3 , 4 , 5 ]
N = len (arr)
countPairs(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void countPairs( int []arr, int N)
{
int answer = 0;
Dictionary< int , int > mp = new Dictionary< int , int >();
for ( int i = 0; i < N; i++) {
int keyValue = arr[i] - i;
if (mp.ContainsKey(keyValue))
mp[keyValue]++;
else
mp.Add(keyValue,1);
}
foreach (KeyValuePair< int , int > entry in mp)
{
int size = entry.Value;
answer += (size * (size - 1)) / 2;
}
Console.Write(answer);
}
public static void Main()
{
int []arr = {0, 1, 2, 3, 4, 5 };
int N = arr.Length;
countPairs(arr, N);
}
}
|
Javascript
<script>
function countPairs(arr, N) {
let answer = 0;
let mp = new Map();
for (let i = 0; i < N; i++) {
let keyValue = arr[i] - i;
if (mp.has(keyValue)) {
mp.set(keyValue, mp.get(keyValue) + 1);
} else {
mp.set(keyValue, 1);
}
}
for (let i of mp) {
let size = i[1];
answer += (size * (size - 1)) / 2;
}
document.write(answer);
}
let arr = [0, 1, 2, 3, 4, 5];
let N = arr.length;
countPairs(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...