


If L = 5 and R = 7, and you need to find the 1’st special number lying in the range [5, 7]
The answer for this is equal to 5, as 5 is the first special number lying in the given range, it is a special number because it has a subarray 101 in its binary representation.
The first line contains a single integer ‘T’ denoting the number of test cases, then each test case follows:
The first line of each test case contains three integers ‘L’, ‘R’ and ‘K’ denoting the range and the lucky number to be calculated respectively.
For each test case, print the K’th special number lying in the given range.
Output for each test case will be printed in a separate line.
You are not required to print anything; it has already been taken care of. Just implement the function.
1 ≤ T ≤ 10
1 ≤ L ≤ R ≤ 50000
1 ≤ K ≤ 50000
Time limit: 1 sec
A simple straightforward approach is to check the binary representation of each number in the range, as soon as we have found the K’th special number we can print it.
The steps are as follows :
This problem involves the knowledge of digit DP, refer to some resource/blog if this is your first encounter with this topic.
We will first build logic to calculate the number of special numbers in the range [0, X]. then we can calculate the number of special numbers in the range [0, L], let the count of these numbers be ‘cnt’, now we need to find the smallest number Y such that the range [0, Y] has at least ‘cnt’ + K special numbers. You are halfway done if you were able to figure out this, as this situation can easily be tackled by binary search. Why? Because the count of special numbers in the range [0, Y] is a monotonically increasing function and using binary search we can calculate the smallest value of Y for which the count is equal to ‘cnt’ + K.
We are now left with a method to find the number of special numbers in a range [0, Y] optimally. It may be possible for someone who is well versed with digit DP to figure out a way to tackle this, but with no prior knowledge, it is not likely you will be able to figure out the logic. To start with what digit DP actually is, we try to build a sequence (binary sequence here) from left to right. And see the possibility of placing the digit on the current position based on the fact that there was a matching prefix in the previous state or not, we do this to avoid the possibility to build a number that is greater than the right bound of our range. Therefore we need one state to keep the track of the digit currently we are working on and one state to check if there is a matching prefix or not, along with this we need additional states according to conditions in the question but for every digit DP question the two states discussed till now remain common. For this question, we need to store additional information whether we have already encountered a 101 subarray or not, we can use ‘ctr’ to store this, additionally, we need information about the last two digits, ie: if we are currently placing the i’th digit then we need information about the i+1’th and i+2’th digit, as if these are 1 and 0 respectively then placing 1 at the i’th position will generate a 101 subarray and thus we can set ‘ctr’ equal to one. We can implement this logic recursively, starting for the 31’st bit as all the input numbers are in the integer range, we can make recursive calls and end the recursion when we have reached the -1’th bit, (in short: we will try to fill all the bits from 31’st to 0’th).
The steps are as follows :