Count pairs in array such that one element is reverse of another
Last Updated :
05 Jan, 2023
Given an array arr[], the task is to count the pairs in the array such that the elements are the 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
Method 1:
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, they return or print the count of such pairs.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int reverse( int num)
{
int rev_num = 0;
while (num > 0) {
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return rev_num;
}
int countReverse( int arr[], int n)
{
int res = 0;
for ( int i = 0; i < n; i++)
for ( int j = i + 1; j < n; j++)
if (reverse(arr[i]) == arr[j]) {
res++;
}
return res;
}
int main()
{
int a[] = { 16, 61, 12, 21, 25 };
int n = sizeof (a) / sizeof (a[0]);
cout << countReverse(a, n);
return 0;
}
|
Java
class Geeks {
static int reverse( int num) {
int rev_num = 0 ;
while (num > 0 ) {
rev_num = rev_num * 10 + num % 10 ;
num = num / 10 ;
}
return rev_num;
}
static int countReverse( int arr[], int n) {
int res = 0 ;
for ( int i = 0 ; i < n; i++)
for ( int j = i + 1 ; j < n; j++)
if (reverse(arr[i]) == arr[j]) {
res++;
}
return res;
}
public static void main(String[] args) {
int a[] = { 16 , 61 , 12 , 21 , 25 };
int n = a.length;
System.out.print(countReverse(a, n));
}
}
|
Python3
def reverse(num):
rev_num = 0
while (num > 0 ):
rev_num = rev_num * 10 + num % 10
num = num / / 10
return rev_num
def countReverse(arr,n):
res = 0
for i in range (n):
for j in range (i + 1 , n):
if (reverse(arr[i]) = = arr[j]):
res + = 1
return res
if __name__ = = '__main__' :
a = [ 16 , 61 , 12 , 21 , 25 ]
n = len (a)
print (countReverse(a, n))
|
C#
using System;
class GFG
{
static int reverse( int num)
{
int rev_num = 0;
while (num > 0) {
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return rev_num;
}
static int countReverse( int []arr, int n)
{
int res = 0;
for ( int i = 0; i < n; i++)
for ( int j = i + 1; j < n; j++)
if (reverse(arr[i]) == arr[j]) {
res++;
}
return res;
}
public static void Main(String []arr)
{
int []a = { 16, 61, 12, 21, 25 };
int n = a.Length;
Console.Write(countReverse(a, n));
}
}
|
Javascript
<script>
function reverse(num)
{
var rev_num = 0;
while (num > 0) {
rev_num = rev_num * 10 + num % 10;
num = parseInt(num / 10);
}
return rev_num;
}
function countReverse(arr, n)
{
var res = 0;
for ( var i = 0; i < n; i++)
for ( var j = i + 1; j < n; j++)
if (reverse(arr[i]) == arr[j]) {
res++;
}
return res;
}
var a = [ 16, 61, 12, 21, 25 ];
var n = a.length;
document.write( countReverse(a, n));
</script>
|
Time Complexity: O(N2 * log10M), where N is the size of the given array and M is the maximum element in the array.
Auxiliary Space: O(1)
Method 2: (Using Hash-Map)
We can observe that the most expensive operation here is searching for the reversed element in the array(which takes O(N)). By using a hash map, this can be reduced to O(1).
Approach: The idea is to store all the elements of the array in the hash map(by increasing the frequency of the present element to tackle the problem of duplicates) and check how many times the reversed element is repeated and increase the count by that frequency. To avoid recounting the number when it is a palindrome or when we visit its reverse, we need to delete the present number from the hash map(this is achieved by decreasing the frequency of that number).
C++
#include <bits/stdc++.h>
using namespace std;
int reverse( int num)
{
int rev_num = 0;
while (num > 0) {
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return rev_num;
}
int countReverse( int arr[], int n)
{
unordered_map< int , int > freq;
for ( int i = 0; i < n; ++i)
++freq[arr[i]];
int res = 0;
for ( int i = 0; i < n; i++){
--freq[arr[i]];
res += freq[reverse(arr[i])];
}
return res;
}
int main() {
int a[] = { 16, 61, 12, 21, 25 };
int n = sizeof (a) / sizeof (a[0]);
cout << countReverse(a, n) << '\n' ;
return 0;
}
|
Java
import java.util.*;
class GFG{
public static int reverse( int num)
{
int rev_num = 0 ;
while (num > 0 )
{
rev_num = rev_num * 10 +
num % 10 ;
num = num / 10 ;
}
return rev_num;
}
public static int countReverse( int arr[],
int n)
{
HashMap<Integer,
Integer> freq =
new HashMap<>();
for ( int i = 0 ; i < n; ++i)
{
if (freq.containsKey(arr[i]))
{
freq.replace(arr[i],
freq.get(arr[i]) + 1 );
}
else
{
freq.put(arr[i], 1 );
}
}
int res = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (freq.containsKey(arr[i]))
{
freq.replace(arr[i],
freq.get(arr[i]) - 1 );
}
else
{
freq.put(arr[i], - 1 );
}
if (freq.containsKey(reverse(arr[i])))
{
res += freq.get(reverse(arr[i]));
}
}
return res;
}
public static void main(String[] args)
{
int a[] = { 16 , 61 , 12 , 21 , 25 };
int n = a.length;
System.out.println(countReverse(a, n));
}
}
|
Python3
from collections import defaultdict
def reverse(num):
rev_num = 0
while (num > 0 ):
rev_num = rev_num * 10 + num % 10
num = num / / 10
return rev_num
def countReverse(arr, n):
freq = defaultdict ( int )
for i in range (n):
freq[arr[i]] + = 1
res = 0
for i in range (n):
freq[arr[i]] - = 1
res + = freq[reverse(arr[i])]
return res
if __name__ = = "__main__" :
a = [ 16 , 61 , 12 , 21 , 25 ]
n = len (a)
print (countReverse(a, n))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int reverse( int num)
{
int rev_num = 0;
while (num > 0)
{
rev_num = rev_num * 10 +
num % 10;
num = num / 10;
}
return rev_num;
}
static int countReverse( int [] arr, int n)
{
Dictionary< int ,
int > freq = new Dictionary< int ,
int >();
for ( int i = 0; i < n; ++i)
{
if (freq.ContainsKey(arr[i]))
{
freq[arr[i]]++;
}
else
{
freq.Add(arr[i], 1);
}
}
int res = 0;
for ( int i = 0; i < n; i++)
{
if (freq.ContainsKey(arr[i]))
{
freq[arr[i]]--;
}
else
{
freq.Add(arr[i], -1);
}
if (freq.ContainsKey(reverse(arr[i])))
{
res += freq[reverse(arr[i])];
}
}
return res;
}
static void Main()
{
int [] a = { 16, 61, 12, 21, 25 };
int n = a.Length;
Console.WriteLine(countReverse(a, n));
}
}
|
Javascript
<script>
function reverse(num)
{
let rev_num = 0;
while (num > 0)
{
rev_num = rev_num * 10 +
num % 10;
num = Math.floor(num / 10);
}
return rev_num;
}
function countReverse(arr,n)
{
let freq = new Map();
for (let i = 0; i < n; ++i)
{
if (freq.has(arr[i]))
{
freq.set(arr[i],
freq.get(arr[i]) + 1);
}
else
{
freq.set(arr[i], 1);
}
}
let res = 0;
for (let i = 0; i < n; i++)
{
if (freq.has(arr[i]))
{
freq.set(arr[i],
freq.get(arr[i]) - 1);
}
else
{
freq.set(arr[i], -1);
}
if (freq.has(reverse(arr[i])))
{
res += freq.get(reverse(arr[i]));
}
}
return res;
}
let a=[16, 61, 12, 21, 25];
let n = a.length;
document.write(countReverse(a, n));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...