Count of numbers in the range [L, R] which satisfy the given conditions
Given a range [L, R], the task is to find the count of numbers from this range that satisfy the below conditions:
- All the digit in the number are distinct.
- All the digits are less than or equal to 5.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Input: L = 4, R = 13
4, 5, 10, 12 and 13 are the only
valid numbers in the range [4, 13].
Input: L = 100, R = 1000
Approach: The question seems simple if the range is small because in that case, all the numbers from the range can be iterated and checked whether they are valid or not. But since the range could be large, it can be observed all the digits of a valid number has to be distinct and from the range [0, 5] which suggests that the maximum number cannot exceed 543210.
Now instead of checking for every number, the next valid number in the series can be generated from the previously generated numbers. The idea is similar to the approach discussed here.
Below is the implementation of the above approach: