# Number of recycled pairs in an array

Given an array of integers arr[], find the number of recycled pairs in the array. A recycled pair of two numbers {a, b} has the following properties :

- A should be smaller than B.
- Number of digits should be same.
- By rotating A any number of times in one direction, we should get B.

Examples:

Input: arr[] = {32, 42, 13, 23, 9, 5, 31}Output: 2Explanation: Since there are two pairs {13, 31} and {23, 32}. By rotating 13 for first time, output is 31 and by rotating 23 once output is 32. Both of these pairs satisfy our criteria.Input: arr[] = {1212, 2121}Output: 1Explanation: Since there are two pairs {1212, 2121}. By rotating 1212 for first time, output is 2121. This pair satisfies our criteria. Note that if rotation id done further, rotating 1212 again output is 1212 which is given number and 2121 which has been already counted. So discard both of these results.

Below is the step by step algorithm to solve the above problem:

- Sort the array.
- Create a new array ‘temp’ of size n where n is the length of original array.
- Remove duplicates from the array by copying unique values to new array ‘temp’.
- Find the number of elements copied from original array and let this number be the size of array.
- Create a HashSet to store only unique rotations of the current number.
- Initialize a counter with value = 0.
- Traverse ‘temp’ and for every number do the following steps –
- Find the number of digits. Let it be ‘d1’.
- Rotate the number for d-1 times and store every number formed by each rotation in a HashSet.
- If formed number is found in HashSet, ignore it.
- For every rotated number, do a binary search for its presence in rest of the array.
- If it is present, increment counter.

`// Java code for Recycled Pairs in array. ` `import` `java.util.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to find recycled pairs ` ` ` `static` `int` `recycledPairs(` `int` `[] a) ` ` ` `{ ` ` ` `int` `count = ` `0` `; ` ` ` ` ` `// Sorting array ` ` ` `Arrays.sort(a); ` ` ` `int` `n = a.length; ` ` ` ` ` `// Removing duplicates by creating new array temp. ` ` ` `int` `[] temp = ` `new` `int` `[n]; ` ` ` `Arrays.fill(temp, -` `1` `); ` ` ` `int` `j = ` `0` `; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < n - ` `1` `; i++) ` ` ` `if` `(a[i] != a[i + ` `1` `]) ` ` ` `temp[j++] = a[i]; ` ` ` `temp[j++] = a[n - ` `1` `]; ` ` ` `int` `size = n; ` ` ` ` ` `// Finding number of locations in temp which are occupied from copying. ` ` ` `for` `(` `int` `i = n - ` `1` `; i >= ` `0` `; i--) ` ` ` `if` `(temp[i] != -` `1` `) { ` ` ` `size = i; ` ` ` `break` `; ` ` ` `} ` ` ` ` ` `// Hashset to store new Rotations ` ` ` `HashSet<Integer> hs = ` `new` `HashSet<Integer>(); ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < size + ` `1` `; i++) { ` ` ` ` ` `// Clearing hashset for each number in temp. ` ` ` `hs.clear(); ` ` ` `int` `x = temp[i]; ` ` ` ` ` `// Finding number of digits of taken number ` ` ` `int` `d1 = (` `int` `)Math.log10(temp[i]) + ` `1` `; ` ` ` ` ` `int` `f = (` `int` `)Math.pow(` `10` `, d1 - ` `1` `); ` ` ` `for` `(j = ` `1` `; j <= d1 - ` `1` `; j++) { ` ` ` ` ` `// Remainder ` ` ` `int` `r = x % ` `10` `; ` ` ` ` ` `// Quotient ` ` ` `int` `q = x / ` `10` `; ` ` ` ` ` `// Forming new number by rotating. ` ` ` `x = r * f + q; ` ` ` ` ` `// Number of digits of newly formed rotated number ` ` ` `// to avoid duplicate numbers. ` ` ` `int` `d2 = (` `int` `)Math.log10(x) + ` `1` `; ` ` ` ` ` `// Inserting formed rotated number to set s ` ` ` `if` `(!hs.contains(x)) { ` ` ` `hs.add(x); ` ` ` ` ` `// Checking for number of digits of new number. ` ` ` `if` `((d1 == d2)) ` ` ` `{ ` ` ` `// Searching for the formed element in rest of array. ` ` ` `int` `position = Arrays.binarySearch(temp, i + ` `1` `, size + ` `1` `, x); ` ` ` ` ` `// If position found ` ` ` `if` `(position >= ` `0` `) ` ` ` `{ ` ` ` `// Increment counter. ` ` ` `count++; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return counter ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver function ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `a[] = { ` `32` `, ` `42` `, ` `13` `, ` `23` `, ` `9` `, ` `5` `, ` `31` `}; ` ` ` `int` `result = recycledPairs(a); ` ` ` `System.out.println(result); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

2

**Time Complexity** : O(n*log(n)).

**Note**: For any given integer, the maximum number of rotations to form new numbers are fixed that is (no_of_digits-1). Hence, this operation is constant time that is O(1).

## Recommended Posts:

- Number of unique pairs in an array
- Find number of pairs in an array such that their XOR is 0
- Number of prime pairs in an array
- Number of pairs in an array such that product is greater than sum
- Count number of distinct pairs whose sum exists in the given array
- Find the maximum cost of an array of pairs choosing at most K pairs
- Find pairs in array whose sums already exist in array
- Sum of XOR of sum of all pairs in an array
- Sum of XOR of all pairs in an array
- Count pairs in an array such that LCM(arr[i], arr[j]) > min(arr[i],arr[j])
- Find all pairs (a, b) in an array such that a % b = k
- Sum of f(a[i], a[j]) over all pairs in an array of n integers
- Count of pairs (x, y) in an array such that x < y
- Sum of Bitwise OR of all pairs in a given array
- Count pairs in a sorted array whose sum is less than x

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.