Count Distinct Integers After Reverse Operations
Add the digit reversal of every number to an array, then count how many different values it holds.
You are given an array nums
Example 1
Output: 6
Explanation: the reversed numbers are 1, 31, 1, 21 and 13 (10 reversed is 01, which is 1). The different values are 1, 10, 12, 13, 21 and 31.
Example 2
Output: 1
Explanation: every number reverses to 2, so the final array holds six 2s.
The Idea
Reversing a number means reading its digits from right to left: 13 becomes 31, and 12 becomes 21. A number like 10 reads as 01, and a zero in front of a number does not count, so 10 reversed is just 1.
Take Example 1. The array starts as 1, 13, 10, 12, 31, and reversing each number adds 1, 31, 1, 21, 13 to the end. That makes ten numbers, but many of them repeat. Look at the picture below.
The top row holds the original numbers, and each arrow leads to that number reversed. The orange arrow starts at 10, whose reversal 01 is 1. Most reversed numbers, drawn dashed and marked "already there", are values the array already holds: 1 is the first number, and 31 and 13 are both in the top row. Only 21, in green, is a new value. The bottom row lists every different value once, six in all.
So the question is not really about building the final, longer array. It asks how many different values we meet while looking at each number and its reversal. The different values of a list, each counted once however often it repeats, are called its distinct values, and their number is the answer.
Solution Approach
Turning that idea into code means settling two things: how to count each value only once, and how to reverse a number.
Counting each value once
Do we need to build the longer array at all? The simple way is to build it and then, for each value, check whether it appeared earlier. For 10,000 numbers the longer array has 20,000 values, and checking each one against all the values before it takes about 200 million comparisons.
A HashSet
Reversing a number
How do we reverse the digits? Turning the number into a Stringxr
is the last digit ofx % 10 , the remainder after dividing by 10.x shifts the digits ofr * 10 + digit one place left and puts the digit at the end.r drops the last digit fromx / 10 , because integer division throws away the remainder.x
For 13, the first round gives r = 3x = 1r = 31x = 0r = 0 * 10 + 0 = 0r = 0 * 10 + 1 = 1
Java Solution
The method countDistinctIntegers
- First, create an empty
calledHashSet to hold the distinct values.seen - Loop over the array, taking each number
in turn, and addx itself tox .seen - Also add
. The helper methodreverse(x) startsreverse at 0 and, whiler is above 0, moves the last digit ofx to the end ofx , then returnsr .r - When the loop is done, return
, the number of distinct values.seen.size()
The main[10][100, 1][121][1000000]
import java.util.HashSet;
import java.util.Set;
public class CountDistinctIntegersAfterReverseOperations {
public static int countDistinctIntegers(int[] nums) {
Set<Integer> seen = new HashSet<>();
for (int x : nums) {
seen.add(x);
seen.add(reverse(x));
}
return seen.size();
}
private static int reverse(int x) {
int r = 0;
while (x > 0) {
r = r * 10 + x % 10; // move the last digit of x to the end of r
x /= 10;
}
return r;
}
public static void main(String[] args) {
System.out.println(countDistinctIntegers(new int[] {1, 13, 10, 12, 31}));
System.out.println(countDistinctIntegers(new int[] {2, 2, 2}));
System.out.println(countDistinctIntegers(new int[] {10}));
System.out.println(countDistinctIntegers(new int[] {100, 1}));
System.out.println(countDistinctIntegers(new int[] {121}));
System.out.println(countDistinctIntegers(new int[] {1000000}));
System.out.println(reverse(13) + " " + reverse(10) + " " + reverse(120) + " " + reverse(7));
}
}
Running it prints:
6
1
2
2
1
2
31 1 21 7
The 6 and the 1 at the top match the two examples. The third line, 2, comes from [10]
The fourth line is also 2. Both 100 and 1 reverse to 1, so the set holds only 100 and 1. The palindrome 121 gives 1, since its reversal is 121 again. The sixth line, 2, comes from [1000000]
Complexity
The work is one pass over the array, with a short digit loop and two set additions for each number.
- Time: O(n × d), where n is the length of the array and d is the number of digits in the largest number.
runs its loop once per digit, and eachreverse addition takes constant time on average. Numbers up to 1,000,000 have at most 7 digits, so this is close to O(n).HashSet - Space: O(n) for the set, which holds at most 2n values, one original and one reversal per number.
The same digit reversal also decides whether a number is a palindrome: see