Given an integer N, the task is to count every number i from 1 to N (both inclusive) such that i is a binary representation of some integer where N can be any value within the range[1, 109]
Input: N = 100
Explanation: Valid integers are 1, 10, 11, 100
Input: N = 20
Explanation: Valid integers are 1, 10, 11
Naive approach: Since maximum number of digits in N can be 10 so store every binary combination of 10 digits and then use Binary search or upper_bound to check the largest integer in the given range of N.
Time Complexity: O(MAX + log(MAX)) where MAX = 1024 (210)
Efficient approach: We can observe that for any value of N, the maximum number of such possible representations is 2count of digits of N – 1. Hence, we need to follow the following steps:
- Extract digits of N from right to left and store the position of the current digit in a variable ctr.
- If the current digit exceeds 1, it means that maximum possible representations using ctr digits can be obtained. Thus, set answer equal to 2ctr – 1.
- Otherwise, if the current digit is 1, then add 2ctr – 1 to the answer obtained so far.
- The final value obtained after traversing all the digits gives the answer.
Below is the implementation of the above approach:
Time Complexity: O(M2) where M is the count of digits in N
Auxilary Space: O(1)
Optimization: The above approach can be optimized by pre-computing the powers of 2 up to M (count of digits up to M of N) by the help of a prefix product array.
Below is the implementation of the optimized solution:
Time Complexity: O(M)
Auxilary Space: O(M)
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Maximum number of distinct positive integers that can be used to represent N
- Count of matchsticks required to represent the given number
- Count of ways to represent N as sum of a prime number and twice of a square
- Different ways to represent N as sum of K non-zero integers
- Represent (2 / N) as the sum of three distinct positive integers of the form (1 / m)
- Number of mismatching bits in the binary representation of two integers
- Count number of integers less than or equal to N which has exactly 9 divisors
- Count ways to express even number ‘n’ as sum of even integers
- Add two integers of different base and represent sum in smaller base of the two
- Count number of trailing zeros in Binary representation of a number using Bitset
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Count of sticks required to represent the given string
- Minimum count of numbers required from given array to represent S
- Sort numbers based on count of letters required to represent them in words
- Number of distinct ways to represent a number as sum of K unique primes
- Count number of binary strings of length N having only 0's and 1's
- Count number of binary strings without consecutive 1’s : Set 2
- Minimum number of given powers of 2 required to represent a number
- Count of primes after converting given binary number in base between L to R
- C program to count zeros and ones in binary representation of a number
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.