Count pairs in given Array having sum of index and value at that index equal
Last Updated :
21 Jul, 2022
Given an array arr[] containing positive integers, count the total number of pairs for which arr[i]+i = arr[j]+j such that 0≤i<j≤n-1.
Examples:
Input: arr[] = { 6, 1, 4, 3 }
Output: 3
Explanation: The elements at index 0, 2, 3 has same value of a[i]+i as all sum to 6 {(6+0), (4+2), (3+3)}.
Input: arr[] = { 8, 7, 6, 5, 4, 3, 2, 1 }
Output: 28
Naive Approach: The brute force approach is implemented by iterating two loops and counting all such pairs that follow arr[i]+i = arr[j]+j.
Below is the implementation of the above approach:
C++
// C++ program to find Count the pair of
// elements in an array such that
// arr[i]+i=arr[j]+j
#include <bits/stdc++.h>
using namespace std;
// Function to return the
// total count of pairs
int count(int arr[], int n)
{
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if ((arr[i] + i) == (arr[j] + j)) {
ans++;
}
}
}
return ans;
}
// Driver code
int main()
{
int arr[] = { 6, 1, 4, 3 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << count(arr, N);
return 0;
}
Java
// Java program to find Count the pair of
// elements in an array such that
// arr[i]+i=arr[j]+j
import java.io.*;
class GFG
{
// Function to return the
// total count of pairs
static int count(int arr[], int n)
{
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if ((arr[i] + i) == (arr[j] + j)) {
ans++;
}
}
}
return ans;
}
// Driver code
public static void main (String[] args) {
int arr[] = { 6, 1, 4, 3 };
int N = arr.length;
System.out.println(count(arr, N));
}
}
// This code is contributed by hrithikgarg03188.
Python3
# python3 program to find Count the pair of
# elements in an array such that
# arr[i]+i=arr[j]+j
# Function to return the
# total count of pairs
def count(arr, n):
ans = 0
for i in range(0, n):
for j in range(i + 1, n):
if ((arr[i] + i) == (arr[j] + j)):
ans += 1
return ans
# Driver code
if __name__ == "__main__":
arr = [6, 1, 4, 3]
N = len(arr)
print(count(arr, N))
# This code is contributed by rakeshsahni
C#
// C# program to find Count the pair of
// elements in an array such that
// arr[i]+i=arr[j]+j
using System;
class GFG {
// Function to return the
// total count of pairs
static int count(int[] arr, int n)
{
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if ((arr[i] + i) == (arr[j] + j)) {
ans++;
}
}
}
return ans;
}
// Driver code
public static int Main()
{
int[] arr = new int[] { 6, 1, 4, 3 };
int N = arr.Length;
Console.WriteLine(count(arr, N));
return 0;
}
}
// This code is contributed by Taranpreet
JavaScript
<script>
// Javascript program to find Count the pair of
// elements in an array such that
// arr[i]+i=arr[j]+j
// Function to return the
// total count of pairs
function count(arr, n)
{
let ans = 0;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
if ((arr[i] + i) == (arr[j] + j)) {
ans++;
}
}
}
return ans;
}
// Driver code
let arr = [ 6, 1, 4, 3 ];
let N = arr.length;
document.write(count(arr, N));
// This code is contributed by Samim Hossain Mondal.
</script>
Time complexity: O(N^2)
Auxiliary Space: O(1)
Efficient Approach: This problem can be efficiently solved by using unordered_map in C++. This is done to store the similar elements count in an average time of O(1). Then for each similar element, we can use the count of that element to evaluate the total number of pairs, as
For x similar items => number of pairs will be x*(x-1)/2
Below is the implementation of the above approach:
C++
// C++ program to find Count the pair of
// elements in an array such that
// arr[i]+i=arr[j]+j
#include <bits/stdc++.h>
using namespace std;
// Function to return the
// total count of pairs
int count(int arr[], int n)
{
// Modifying the array by storing
// a[i]+i at all instances
for (int i = 0; i < n; i++) {
arr[i] = arr[i] + i;
}
// Using unordered_map to store
// the elements
unordered_map<int, int> mp;
for (int i = 0; i < n; i++) {
mp[arr[i]]++;
}
// Now for each elements in unordered_map
// we are using the count of that element
// to determine the number of pairs possible
int ans = 0;
for (auto it = mp.begin(); it != mp.end(); it++) {
int val = it->second;
ans += (val * (val - 1)) / 2;
}
return ans;
}
// Driver code
int main()
{
int arr[] = { 8, 7, 6, 5, 4, 3, 2, 1 };
int N = sizeof(arr) / sizeof(arr[0]);
cout << count(arr, N);
return 0;
}
Java
/*package whatever //do not write package name here */
// Java program to find Count the pair of
// elements in an array such that
// arr[i]+i=arr[j]+j
import java.io.*;
import java.util.*;
class GFG {
// Function to return the
// total count of pairs
static int count(int arr[], int n)
{
// Modifying the array by storing
// a[i]+i at all instances
for (int i = 0; i < n; i++) {
arr[i] = arr[i] + i;
}
// Using unordered_map to store
// the elements
HashMap<Integer,Integer>mp = new HashMap<Integer,Integer>();
for (int i = 0; i < n; i++) {
if(mp.containsKey(arr[i])){
mp.put(arr[i], mp.get(arr[i])+1);
}
else mp.put(arr[i],1);
}
// Now for each elements in unordered_map
// we are using the count of that element
// to determine the number of pairs possible
int ans = 0;
for(int it : mp.keySet()){
ans += (mp.get(it)*(mp.get(it)-1))/2;
}
return ans;
}
// Driver code
public static void main(String args[])
{
int arr[] = { 8, 7, 6, 5, 4, 3, 2, 1 };
int N = arr.length;
System.out.println(count(arr, N));
}
}
// This code is contributed by shinjanpatra
Python3
# Python program to find Count the pair of
# elements in an array such that
# arr[i]+i=arr[j]+j
# Function to return the
# total count of pairs
def count(arr, n):
# Modifying the array by storing
# a[i]+i at all instances
for i in range(n):
arr[i] = arr[i] + i
# Using unordered_map to store
# the elements
mp = {}
for i in range(n):
if(arr[i] in mp):
mp[arr[i]] = mp[arr[i]]+1
else:
mp[arr[i]] = 1
# Now for each elements in unordered_map
# we are using the count of that element
# to determine the number of pairs possible
ans = 0
for val in mp.values():
ans += (val * (val - 1)) // 2
return ans
# Driver code
arr = [ 8, 7, 6, 5, 4, 3, 2, 1 ]
N = len(arr)
print(count(arr, N))
# This code is contributed by shinjanpatra
C#
// C# program to find Count the pair of
// elements in an array such that
// arr[i]+i=arr[j]+j
using System;
using System.Collections.Generic;
class GFG {
// Function to return the
// total count of pairs
static int count(int[] arr, int n)
{
// Modifying the array by storing
// a[i]+i at all instances
for (int i = 0; i < n; i++) {
arr[i] = arr[i] + i;
}
// Using unordered_map to store
// the elements
Dictionary<int, int> mp = new Dictionary<int, int>();
for (int i = 0; i < n; i++) {
if(mp.ContainsKey(arr[i])){
mp[arr[i]] = mp[arr[i]] + 1;
}
else mp.Add(arr[i], 1);
}
// Now for each elements in unordered_map
// we are using the count of that element
// to determine the number of pairs possible
int ans = 0;
foreach(KeyValuePair<int, int> it in mp){
ans += (it.Value*(it.Value-1))/2;
}
return ans;
}
// Driver code
public static int Main()
{
int[] arr = new int[] { 8, 7, 6, 5, 4, 3, 2, 1 };
int N = arr.Length;
Console.WriteLine(count(arr, N));
return 0;
}
}
// This code is contributed by Aman Kumar
JavaScript
<script>
// JavaScript program to find Count the pair of
// elements in an array such that
// arr[i]+i=arr[j]+j
// Function to return the
// total count of pairs
function count(arr, n)
{
// Modifying the array by storing
// a[i]+i at all instances
for (let i = 0; i < n; i++) {
arr[i] = arr[i] + i;
}
// Using unordered_map to store
// the elements
let mp = new Map();
for (let i = 0; i < n; i++) {
if(mp.has(arr[i])){
mp.set(arr[i], mp.get(arr[i]) + 1);
}
else mp.set(arr[i], 1);
}
// Now for each elements in unordered_map
// we are using the count of that element
// to determine the number of pairs possible
let ans = 0;
for (let [key,val] of mp){
ans += Math.floor((val * (val - 1)) / 2);
}
return ans;
}
// Driver code
let arr = [ 8, 7, 6, 5, 4, 3, 2, 1 ];
let N = arr.length;
document.write(count(arr, N));
// This code is contributed by shinjanpatra
</script>
Time complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Count pair of indices in Array having equal Prefix-MEX and Suffix-MEX Given an array arr[] of N elements, the task is to find the number of pairs of indices such that the Prefix-MEX of the one index is equal to the Suffix-MEX of the other index. Order of indices in pair matters. MEX of an array refers to the smallest missing non-negative integer of the array. For the
10 min read
Number of Good Pairs - Count of index pairs with equal values in an array Given an array of n elements. The task is to count the total number of indices (i, j) such that arr[i] = arr[j] and i < jExamples : Input: arr = [5, 2, 3, 5, 5, 3]Output: 4Explanation: There are 4 good pairs (0, 3), (0, 4), (3, 4), (2, 5)Input: arr = [1, 1, 1, 1]Output: 6Explanation: All 6 pairs
6 min read
Count of pairs (arr[i], arr[j]) such that arr[i] + j and arr[j] + i are equal Given an array arr[], the task is to count pairs i, j such that, i < j and arr[i] + j = arr[j] + i. Examples: Input: arr[] = {4, 1, 2, 3}Output: 3Explanation: In total three pairs are satisfying the given condition those are {1, 2}, {2, 3} and {1, 3}.So, the final answer is 3. Input: arr[] = {1,
5 min read
Count of indices up to which prefix and suffix sum is equal for given Array 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: 2Explanation: The indices up to which prefix and suffix sum are equal are given below:At index 1 prefix and suffix sum are 9
15+ min read
Count the number of ways to divide an array into three contiguous parts having equal sum Given an array of n numbers. Our task is to find out the number of ways to divide the array into three contiguous parts such that the sum of three parts is equal. In other words, we need to find the number of index pairs i and j such that sum of elements from 0 to i-1 is equal to the sum of elements
15+ min read