Given a limit N, we need to find out the count of binary digit numbers which are smaller than N. Binary digit numbers are those numbers which contains only 0 and 1 as their digits as 1, 10, 101 etc are binary digit numbers.
Input : N = 200 Output : 7 Count of binary digit number smaller than N is 7, enumerated below, 1, 10, 11, 110, 101, 100, 111
One simple way to solve this problem is to loop from 1 till N and check each number whether it is a binary digit number or not. If it is a binary digit number, increase the count of such numbers but this procedure will take O(N) time. We can do better, as we know that count of such numbers will be much smaller than N, we can iterate over binary digit numbers only and check whether generated numbers are smaller than N or not.
In below code, BFS like approach is implemented to iterate over only binary digit numbers. We start with 1 and each time we will push (t*10) and (t*10 + 1) into the queue where t is the popped element, if t is a binary digit number then (t*10) and (t*10 + 1) will also binary digit number, so we will iterate over these numbers only using queue. We will stop pushing elements in the queue when popped number crosses the N.
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Count numbers (smaller than or equal to N) with given digit sum
- Count of N-digit numbers having digit XOR as single digit
- Largest number less than N with digit sum greater than the digit sum of N
- Count n digit numbers not having a particular digit
- Count of Numbers in Range where first digit is equal to last digit of the number
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Print all Jumping Numbers smaller than or equal to a given value
- Euler's Totient function for all numbers smaller than or equal to n
- An interesting solution to get all prime numbers smaller than n
- Cube Free Numbers smaller than n
- Minimum numbers (smaller than or equal to N) with sum S
- Check if frequency of each digit is less than the digit
- Size of smallest subarray to be removed to make count of array elements greater and smaller than K equal
- Count numbers with difference between number and its digit sum greater than specific value
- Count numbers whose maximum sum of distinct digit-sum is less than or equals M
- Count 'd' digit positive integers with 0 as a digit
- Count of pairs (A, B) in range 1 to N such that last digit of A is equal to the first digit of B
- Sieve of Sundaram to print all primes smaller than n
- Largest number smaller than or equal to n and digits in non-decreasing order
- Maximize a number considering permutations with values smaller than limit