Given an array **arr[]**, the task is to count the pairs in the array such that elements are reverse of each other.

**Examples:**

Input:arr[] = { 16, 61, 12, 21, 25 }

Output:2

Explanation:

The 2 pairs such that one number is the reverse of the other are {16, 61} and {12, 21}.

Input:arr[] = {10, 11, 12}

Output:0

**Approach:** The idea is to use nested loops to get all the possible pairs of numbers in the array. Then, for each pair, check whether an element is a reverse of another. If it is, then increase the required count by one. When all the pairs have been checked, return or print the count of such pair.

Below is the implementation of the above approach:

## C++

`// C++ program to count the pairs in array ` `// such that one element is reverse of another ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to reverse the digits ` `// of the number ` `int` `reverse(` `int` `num) ` `{ ` ` ` `int` `rev_num = 0; ` ` ` ` ` `// Loop to iterate till the number is ` ` ` `// greater than 0 ` ` ` `while` `(num > 0) { ` ` ` ` ` `// Extract the last digit and keep ` ` ` `// multiplying it by 10 to get the ` ` ` `// reverse of the number ` ` ` `rev_num = rev_num * 10 + num % 10; ` ` ` `num = num / 10; ` ` ` `} ` ` ` `return` `rev_num; ` `} ` ` ` `// Function to find the pairs from the ` `// such that one number is reverse of ` `// the other ` `int` `countReverse(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `int` `res = 0; ` ` ` ` ` `// Iterate through all pairs ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `for` `(` `int` `j = i + 1; j < n; j++) ` ` ` ` ` `// Increment count if one is ` ` ` `// the reverse of other ` ` ` `if` `(reverse(arr[i]) == arr[j]) { ` ` ` `res++; ` ` ` `} ` ` ` ` ` `return` `res; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a[] = { 16, 61, 12, 21, 25 }; ` ` ` `int` `n = ` `sizeof` `(a) / ` `sizeof` `(a[0]); ` ` ` `cout << countReverse(a, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count the pairs in array ` `// such that one element is reverse of another ` ` ` `class` `Geeks { ` ` ` `// Function to reverse the digits ` ` ` `// of the number ` ` ` `static` `int` `reverse(` `int` `num) { ` ` ` `int` `rev_num = ` `0` `; ` ` ` ` ` `// Loop to iterate till the number is ` ` ` `// greater than 0 ` ` ` `while` `(num > ` `0` `) { ` ` ` ` ` `// Extract the last digit and keep ` ` ` `// multiplying it by 10 to get the ` ` ` `// reverse of the number ` ` ` `rev_num = rev_num * ` `10` `+ num % ` `10` `; ` ` ` `num = num / ` `10` `; ` ` ` `} ` ` ` `return` `rev_num; ` ` ` `} ` ` ` ` ` `// Function to find the pairs from the ` ` ` `// such that one number is reverse of ` ` ` `// the other ` ` ` `static` `int` `countReverse(` `int` `arr[], ` `int` `n) { ` ` ` `int` `res = ` `0` `; ` ` ` ` ` `// Iterate through all pairs ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `for` `(` `int` `j = i + ` `1` `; j < n; j++) ` ` ` ` ` `// Increment count if one is ` ` ` `// the reverse of other ` ` ` `if` `(reverse(arr[i]) == arr[j]) { ` ` ` `res++; ` ` ` `} ` ` ` ` ` `return` `res; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) { ` ` ` `int` `a[] = { ` `16` `, ` `61` `, ` `12` `, ` `21` `, ` `25` `}; ` ` ` `int` `n = a.length; ` ` ` `System.out.print(countReverse(a, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Rajnis09 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count the pairs in array ` `// such that one element is reverse of another ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to reverse the digits ` `// of the number ` `static` `int` `reverse(` `int` `num) ` `{ ` ` ` `int` `rev_num = 0; ` ` ` ` ` `// Loop to iterate till the number is ` ` ` `// greater than 0 ` ` ` `while` `(num > 0) { ` ` ` ` ` `// Extract the last digit and keep ` ` ` `// multiplying it by 10 to get the ` ` ` `// reverse of the number ` ` ` `rev_num = rev_num * 10 + num % 10; ` ` ` `num = num / 10; ` ` ` `} ` ` ` `return` `rev_num; ` `} ` ` ` `// Function to find the pairs from the ` `// such that one number is reverse of ` `// the other ` `static` `int` `countReverse(` `int` `[]arr, ` `int` `n) ` `{ ` ` ` `int` `res = 0; ` ` ` ` ` `// Iterate through all pairs ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `for` `(` `int` `j = i + 1; j < n; j++) ` ` ` ` ` `// Increment count if one is ` ` ` `// the reverse of other ` ` ` `if` `(reverse(arr[i]) == arr[j]) { ` ` ` `res++; ` ` ` `} ` ` ` ` ` `return` `res; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String []arr) ` `{ ` ` ` `int` `[]a = { 16, 61, 12, 21, 25 }; ` ` ` `int` `n = a.Length; ` ` ` `Console.Write(countReverse(a, n)); ` `} ` `} ` ` ` `// This code is contributed by shivanisinghss2110 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count the pairs in array ` `# such that one element is reverse of another ` ` ` `# Function to reverse the digits ` `# of the number ` `def` `reverse(num): ` ` ` `rev_num ` `=` `0` ` ` ` ` `# Loop to iterate till the number is ` ` ` `# greater than 0 ` ` ` `while` `(num > ` `0` `): ` ` ` ` ` `# Extract the last digit and keep ` ` ` `# multiplying it by 10 to get the ` ` ` `# reverse of the number ` ` ` `rev_num ` `=` `rev_num ` `*` `10` `+` `num ` `%` `10` ` ` `num ` `=` `num ` `/` `/` `10` ` ` ` ` `return` `rev_num ` ` ` `# Function to find the pairs from the ` `# such that one number is reverse of ` `# the other ` `def` `countReverse(arr,n): ` ` ` `res ` `=` `0` ` ` ` ` `# Iterate through all pairs ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `for` `j ` `in` `range` `(i ` `+` `1` `, n): ` ` ` ` ` `# Increment count if one is ` ` ` `# the reverse of other ` ` ` `if` `(reverse(arr[i]) ` `=` `=` `arr[j]): ` ` ` `res ` `+` `=` `1` ` ` ` ` `return` `res ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `a ` `=` `[` `16` `, ` `61` `, ` `12` `, ` `21` `, ` `25` `] ` ` ` `n ` `=` `len` `(a) ` ` ` `print` `(countReverse(a, n)) ` ` ` `# This code is contributed by Surendra_Gangwar ` |

*chevron_right*

*filter_none*

**Output:**

2

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 equal element pairs in the given array
- Count unequal element pairs from the given Array
- Count pairs in an array such that at least one element is prime
- Count pairs in array such that one element is power of another
- Count of pairs having each element equal to index of the other from an Array
- Count of pairs from arrays A and B such that element in A is greater than element in B at that index
- Count of distinct possible pairs such that the element from A is greater than the element from B
- Count of pairs with difference at most K with no element repeating
- Count pairs formed by distinct element sub-arrays
- Count pairs in an array such that LCM(arr[i], arr[j]) > min(arr[i],arr[j])
- Count of odd and even sum pairs in an array
- Count of pairs (x, y) in an array such that x < y
- Count pairs in an array containing at least one even value
- Count of pairs in an Array whose sum is Prime
- Count of pairs (i, j) in the array such that arr[i] is a factor of arr[j]
- Count pairs in array whose sum is divisible by 4
- Count pairs in an array such that frequency of one is at least value of other
- Count pairs in array whose sum is divisible by K
- Count pairs in a sorted array whose sum is less than x
- Count divisible pairs in an array

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.