Given an integer **N** and an array **arr[]** that contains elements in the range **[1, N]**, the task is to find the count of all pairs **(arr[i], arr[j])** such that** i < j** and** i == arr[j] and j == arr[i]**.**Examples:**

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

All possible pairs are {1, 2} and {3, 4}Input:N = 5, arr[] = {5, 5, 5, 5, 1}Output:1Explanation:

Only possible pair: {1, 5}

**Naive Approach:**

The simplest approach is to generate all possible pairs of the given array and if any pair satisfies the given condition, increase **count**. Finally, print the value of **count**. **Time Complexity:** O(N^{2}) **Auxiliary Space:** O(1)**Efficient Approach:**

Follow the steps below to solve the above approach:

- Traverse the given array and keep the count of elements(say
**cnt**) whose index equals to**arr[arr[index] – 1] – 1**. This will count the valid pair with the given criteria. - After traversal, the total count is given by
**cnt/2**as we have count every pair twice in the above traversal.

Below is the implementation of the above approach:

## C++

`// C++ program to implement` `// the above approach` `#include<bits/stdc++.h>` `using` `namespace` `std;` `// Function to print the count of pair` `void` `countPairs(` `int` `N, ` `int` `arr[])` `{` ` ` `int` `count = 0;` ` ` `// Iterate over all the` ` ` `// elements of the array` ` ` `for` `(` `int` `i = 0; i < N; i++)` ` ` `{` ` ` `if` `(i == arr[arr[i] - 1] - 1)` ` ` `{` ` ` ` ` `// Increment the count` ` ` `count++;` ` ` `}` ` ` `}` ` ` `// Print the result` ` ` `cout << (count / 2) << endl;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `arr[] = { 2, 1, 4, 3 };` ` ` `int` `N = ` `sizeof` `(arr)/` `sizeof` `(arr[0]);` ` ` `countPairs(N, arr);` `}` `// This code is contributed by Amit Katiyar` |

*chevron_right*

*filter_none*

## Java

`// Java Program to implement` `// the above approach` `import` `java.util.*;` `class` `GFG {` ` ` `// Function to print the count of pair` ` ` `static` `void` `countPairs(` `int` `N, ` `int` `[] arr)` ` ` `{` ` ` `int` `count = ` `0` `;` ` ` `// Iterate over all the` ` ` `// elements of the array` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++) {` ` ` `if` `(i == arr[arr[i] - ` `1` `] - ` `1` `) {` ` ` `// Increment the count` ` ` `count++;` ` ` `}` ` ` `}` ` ` `// Print the result` ` ` `System.out.println(count / ` `2` `);` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `int` `[] arr = { ` `2` `, ` `1` `, ` `4` `, ` `3` `};` ` ` `int` `N = arr.length;` ` ` `countPairs(N, arr);` ` ` `}` `}` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement` `# the above approach` `# Function to print the count of pair` `def` `countPairs(N, arr):` ` ` `count ` `=` `0` ` ` `# Iterate over all the` ` ` `# elements of the array` ` ` `for` `i ` `in` `range` `(N):` ` ` `if` `(i ` `=` `=` `arr[arr[i] ` `-` `1` `] ` `-` `1` `):` ` ` ` ` `# Increment the count` ` ` `count ` `+` `=` `1` ` ` `# Print the result` ` ` `print` `(count ` `/` `/` `2` `)` `# Driver Code` `if` `__name__ ` `=` `=` `"__main__"` `:` ` ` ` ` `arr ` `=` `[` `2` `, ` `1` `, ` `4` `, ` `3` `]` ` ` `N ` `=` `len` `(arr)` ` ` `countPairs(N, arr)` `# This code is contributed by Chitranayal` |

*chevron_right*

*filter_none*

## C#

`// C# Program to implement` `// the above approach` `using` `System;` `class` `GFG{` ` ` ` ` `// Function to print the count of pair` ` ` `static` `void` `countPairs(` `int` `N, ` `int` `[] arr)` ` ` `{` ` ` `int` `count = 0;` ` ` `// Iterate over all the` ` ` `// elements of the array` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `{` ` ` `if` `(i == arr[arr[i] - 1] - 1) ` ` ` `{` ` ` `// Increment the count` ` ` `count++;` ` ` `}` ` ` `}` ` ` `// Print the result` ` ` `Console.Write(count / 2);` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `Main(` `string` `[] args)` ` ` `{` ` ` `int` `[] arr = { 2, 1, 4, 3 };` ` ` `int` `N = arr.Length;` ` ` `countPairs(N, arr);` ` ` `}` `}` `// This code is contributed by Ritik Bansal` |

*chevron_right*

*filter_none*

**Output:**

2

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count of permutations of an Array having each element as a multiple or a factor of its index
- Minimum index i such that all the elements from index i to given index are equal
- Check if array can be sorted by swapping pairs having GCD equal to the smallest element in the array
- Form an array of distinct elements with each element as sum of an element from each array
- Minimize operations required to make each element of Array equal to it's index value
- Count of pairs from arrays A and B such that element in A is greater than element in B at that index
- Range Query on array whose each element is XOR of index value and previous element
- Check if all the pairs of an array are coprime with each other
- Count of index pairs with equal elements in an array
- Count of index pairs with equal elements in an array | Set 2
- Number of ways to select equal sized subarrays from two arrays having atleast K equal pairs of elements
- For each element in 1st array count elements less than or equal to it in 2nd array
- For each element in 1st array count elements less than or equal to it in 2nd array | Set 2
- Check if array can be sorted by swapping pairs with GCD of set bits count equal to that of the smallest array element
- Find the element having different frequency than other array elements
- Sum of product of each element with each element after it
- Largest Divisor for each element in an array other than 1 and the number itself
- Count of contiguous subarrays possible for every index by including the element at that index
- Minimum number of swaps required for arranging pairs adjacent to each other
- Count pairs from two arrays having sum equal to K

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.