Count pairs in an array that hold i+j= arr[i]+arr[j]

Given an array of integers arr[], the task is to count all the pairs (arr[i], arr[j]) such that i + j = arr[i] + arr[j] for all 0 ≤ i < j < n.
Note: Pairs (x, y) and (y, x) are considered a single pair.

Examples:

Input: arr[] = {8, 4, 2, 1, 5, 4, 2, 1, 2, 3}
Output: 1
Only possible pair is (arr[4], arr[5]) i.e. (5, 4)
i + j = arr[i] + arr[j] => 4 + 5 = 5 + 4

Input: arr[] = {1, 0, 3, 2}
Output: 4



Naive Approach: Run two nested loops and check every possible pair for the condition where i + j = arr[i] + arr[j]. If the condition is satisfied then update count = count + 1. Print the count in the end.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to count all the pairs that
// hold the condition i + j = arr[i] + arr[j]
#include <iostream>
using namespace std;
  
// Function to return the count of pairs that
// satisfy the given condition
int CountPairs(int arr[], int n)
{
    int count = 0;
  
    // Generate all possible pairs and increment
    // the count if the condition is satisfied
    for (int i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
            if ((i + j) == (arr[i] + arr[j]))
                count++;
        }
    }
    return count;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 0, 3, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << CountPairs(arr, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to count all the pairs that 
// hold the condition i + j = arr[i] + arr[j] 
  
public class GFG { 
      
    // Function to return the count of pairs that 
    // satisfy the given condition 
    static int CountPairs(int arr[], int n) 
    
        int count = 0
      
        // Generate all possible pairs and increment 
        // the count if the condition is satisfied 
        for (int i = 0; i < n - 1; i++) { 
            for (int j = i + 1; j < n; j++) { 
                if ((i + j) == (arr[i] + arr[j])) 
                    count++; 
            
        
        return count; 
    
      
    // Driver code 
    public static void main(String args[]) 
    
        int arr[] = { 1, 0, 3, 2 }; 
        int n = arr.length ; 
        System.out.print(CountPairs(arr, n)); 
    
  
    // This code is contributed by Ryuga.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to count all the pairs that
# hold the condition i + j = arr[i] + arr[j]
  
# Function to return the count of pairs 
# that satisfy the given condition
def CountPairs(arr, n):
  
    count = 0;
  
    # Generate all possible pairs and increment
    # the count if the condition is satisfied
    for i in range(n - 1):
        for j in range(i + 1, n):
            if ((i + j) == (arr[i] + arr[j])):
                count += 1;
    return count;
  
# Driver code
arr = [ 1, 0, 3, 2 ];
n = len(arr);
print(CountPairs(arr, n));
  
# This code is contributed 
# by Akanksha Rai

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

using System;
  
// C# program to count all the pairs that 
// hold the condition i + j = arr[i] + arr[j] 
   
public class GFG { 
       
    // Function to return the count of pairs that 
    // satisfy the given condition 
    static int CountPairs(int[] arr, int n) 
    
        int count = 0; 
       
        // Generate all possible pairs and increment 
        // the count if the condition is satisfied 
        for (int i = 0; i < n - 1; i++) { 
            for (int j = i + 1; j < n; j++) { 
                if ((i + j) == (arr[i] + arr[j])) 
                    count++; 
            
        
        return count; 
    
       
    // Driver code 
    public static void Main() 
    
        int[] arr = { 1, 0, 3, 2 }; 
        int n = arr.Length ; 
        Console.Write(CountPairs(arr, n)); 
    
   

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to count all the pairs that
// hold the condition i + j = arr[i] + arr[j]
  
// Function to return the count of pairs 
// that satisfy the given condition
function CountPairs(&$arr, $n)
{
    $count = 0;
  
    // Generate all possible pairs and increment
    // the count if the condition is satisfied
    for ($i = 0; $i < $n - 1; $i++) 
    {
        for ($j = $i + 1; $j < $n; $j++) 
        {
            if (($i + $j) == ($arr[$i] + $arr[$j]))
                $count++;
        }
    }
    return $count;
}
  
// Driver code
$arr = array(1, 0, 3, 2 );
$n = sizeof($arr);
echo(CountPairs($arr, $n));
  
// This code is contributed
// by Shivi_Aggarwal 
?>

chevron_right