VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-of-integers-in-given-range-consisting-only-given-set-of-digits/

⇱ Count of Integers in given range consisting only given set of Digits - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Count of Integers in given range consisting only given set of Digits

Last Updated : 23 Jul, 2025

Given two integers L and R, and an array arr[] containing single digit integers, the task is to find all the integers in the range [L, R) consisting of digits from given array of digits. 

Examples:

Input: L = 1, R = 100, arr[] = {2, 3, 5, 7}
Output: 20
Explanation: The number between 1 and 100 total integers which are made up with 2, 3, 5, 7 are:
2, 3, 5, 7, 22, 23, 25, 27, 32, 33, 35, 37, 52, 53, 55, 57, 72, 73, 75, and 77. Total 20.

Input: L = 50, R = 60, arr[] = 5
Output: 1
Explanation: The only number in range 50 and 60 55.

Approach: The solution to the problem is based on greedy approach by using the below idea:

Traverse each integer in range [L, R) and check if it consists of only given set of digits. 

Follow the steps below to solve the problem:

  • Iterate over the range [L, R).
  • Check whether the number is a combination of numbers given in arr[] or not with the help of a set.
  • If it is a subset of given digits then increase count by 1, otherwise not.

Below is the implementation of the above approach:

 
 


Output
20


 

Time Complexity: O((R-L)*logN)
Auxiliary Space: O(1)


 

Comment
Article Tags:
Article Tags: