

If N = 12, then only ‘11’ has repeated digits. Therefore the number of integers between 1 and 12 that have at least one digit repeated = 1.
The first line contains a single integer ‘T’, denoting the number of test cases.
The first line of each test case contains a single integer denoting ‘N’.
For each test case, print a single line containing a single integer denoting the number of integers between 1 and ‘N’ having at least one repeated digit.
The output of each test case will be printed in a separate line.
You don’t need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10 ^ 9
Where ‘T’ is the number of test cases, and ‘N’ is the given integer.
Time limit: 1 sec.
In this approach, We will simply count invalid numbers( numbers with no repeated digits ) and subtract them from the number ‘N’ and get the count of numbers with repeated digits.
We can use bit masking to solve this problem. Let ‘bitmask’ be any integer. In the binary representation of ‘bitmasking’, if the bit at position ‘2^pos’ is ‘1’ then it denotes that the integer contains a digit = pos.
Now if the bitwise ‘and’ operation of ‘bitmask’ with ‘2^d’ is greater than ‘0’, then it denotes that digit ‘d’ is present in the number.
For Example - If a number is ‘13’ then its bitmasking number will be‘10000010’.(2nd,8th bit = 1). Now bitwise and of ‘10000010’ with 2^1 > 0, which denotes digit 1 is present in number.
We will recursively generate all the invalid integers between 1 and N and keep a count of them.
Algorithm
Now, ‘count’ will store the count of integers with non-repeating digits(including 0). So N - count + 1 will give the required answer.
The basic approach that we can use is to reverse the question and instead of counting the numbers with repeated digits, we will simply count numbers with no repeated digits. And using that count, we can subtract it from the number and get the count of numbers with repeated digits.
We will use the permutations formula as it gives us the permutations of unique numbers.
We will do this by varying the digits of the given number and counting the invalid numbers(numbers that have no repeated digits).
Let us understand this approach with the help of an example:
Let N = 450, we have 3 digits in the given number.
To start with we will first find the count of invalid numbers in the range (1 - 99).
There are no numbers of length 1 that can have any repeated digits, so simply add 9 to our count.
Now to get invalid numbers of length 2, we have one digit that we can change. Like if the number is 1X, then X can be varied, 2Y, Y can be varied, and so on.
Now for ‘X’ we have 9 choices ( as we can not use ‘1’ to make number non-repeated).
Using our permutation formula for this, we have permutation(9, 1), where 9 is invalid numbers and 1 is the valid number. For the complete range of numbers with length two, we have, (9 + (permutation(9, 1) * 9)), which gives us 81 invalid numbers. Adding this into our previous count, we have 81 + 9 = 90 invalid numbers in the range of 1 - 99.
Similarly, we move on to count invalid numbers in the range 100-451(to ensure we have N also counted in the range). We do this by varying each of the digits of the numbers and keeping a count.
4XX -> permutation(9-0, 4-0-1) = permutation(9, 3)
X5X -> permutation(9-1, 4-1-1) = permutation(8, 2)
XX1 -> permutation(9-2, 4-2-1) = permutation(7, 1)
We use these permutations to count all the invalid numbers, which in this case will add up to: 339, and hence count of numbers with repeating digits will be :
450 - 339 = 111.