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.

**Examples:**

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.

## C++

`// C++ program to count all binary digit ` `// numbers smaller than N ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// method returns count of binary digit ` `// numbers smaller than N ` `int` `countOfBinaryNumberLessThanN(` `int` `N) ` `{ ` ` ` `// queue to store all intermediate binary ` ` ` `// digit numbers ` ` ` `queue<` `int` `> q; ` ` ` ` ` `// binary digits start with 1 ` ` ` `q.push(1); ` ` ` `int` `cnt = 0; ` ` ` `int` `t; ` ` ` ` ` `// loop untill we have element in queue ` ` ` `while` `(!q.empty()) ` ` ` `{ ` ` ` `t = q.front(); ` ` ` `q.pop(); ` ` ` ` ` `// push next binary digit numbers only if ` ` ` `// current popped element is N ` ` ` `if` `(t <= N) ` ` ` `{ ` ` ` `cnt++; ` ` ` ` ` `// uncomment below line to print actual ` ` ` `// number in sorted order ` ` ` `// cout << t << " "; ` ` ` ` ` `q.push(t * 10); ` ` ` `q.push(t * 10 + 1); ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `cnt; ` `} ` ` ` `// Driver code to test above methods ` `int` `main() ` `{ ` ` ` `int` `N = 200; ` ` ` `cout << countOfBinaryNumberLessThanN(N); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`import` `java.util.LinkedList; ` `import` `java.util.Queue; ` ` ` `// java program to count all binary digit ` `// numbers smaller than N ` `public` `class` `GFG { ` ` ` `// method returns count of binary digit ` `// numbers smaller than N ` ` ` `static` `int` `countOfBinaryNumberLessThanN(` `int` `N) { ` ` ` `// queue to store all intermediate binary ` ` ` `// digit numbers ` ` ` `Queue<Integer> q = ` `new` `LinkedList<>(); ` ` ` ` ` `// binary digits start with 1 ` ` ` `q.add(` `1` `); ` ` ` `int` `cnt = ` `0` `; ` ` ` `int` `t; ` ` ` ` ` `// loop untill we have element in queue ` ` ` `while` `(q.size() > ` `0` `) { ` ` ` `t = q.peek(); ` ` ` `q.remove(); ` ` ` ` ` `// push next binary digit numbers only if ` ` ` `// current popped element is N ` ` ` `if` `(t <= N) { ` ` ` `cnt++; ` ` ` ` ` `// uncomment below line to print actual ` ` ` `// number in sorted order ` ` ` `// cout << t << " "; ` ` ` `q.add(t * ` `10` `); ` ` ` `q.add(t * ` `10` `+ ` `1` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `cnt; ` ` ` `} ` ` ` `// Driver code to test above methods ` ` ` `static` `public` `void` `main(String[] args) { ` ` ` `int` `N = ` `200` `; ` ` ` `System.out.println(countOfBinaryNumberLessThanN(N)); ` ` ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count all binary digit ` `# numbers smaller than N ` `from` `collections ` `import` `deque ` ` ` `# method returns count of binary digit ` `# numbers smaller than N ` `def` `countOfBinaryNumberLessThanN(N): ` ` ` `# queue to store all intermediate binary ` ` ` `# digit numbers ` ` ` `q ` `=` `deque() ` ` ` ` ` `# binary digits start with 1 ` ` ` `q.append(` `1` `) ` ` ` `cnt ` `=` `0` ` ` ` ` `# loop untill we have element in queue ` ` ` `while` `(q): ` ` ` `t ` `=` `q.popleft() ` ` ` ` ` `# push next binary digit numbers only if ` ` ` `# current popped element is N ` ` ` `if` `(t <` `=` `N): ` ` ` `cnt ` `=` `cnt ` `+` `1` ` ` `# uncomment below line to print actual ` ` ` `# number in sorted order ` ` ` `q.append(t ` `*` `10` `) ` ` ` `q.append(t ` `*` `10` `+` `1` `) ` ` ` ` ` `return` `cnt ` ` ` `# Driver code to test above methods ` `if` `__name__` `=` `=` `'__main__'` `: ` ` ` `N ` `=` `200` ` ` `print` `(countOfBinaryNumberLessThanN(N)) ` ` ` `# This code is contributed by ` `# Sanjit_Prasad ` |

*chevron_right*

*filter_none*

## PHP

**Output:**

7

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 contribute@geeksforgeeks.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.

## Recommended Posts:

- Count numbers (smaller than or equal to N) with given digit sum
- Count of Numbers in Range where first digit is equal to last digit of the number
- Count n digit numbers not having a particular digit
- Count numbers having 0 as a digit
- Count numbers from 1 to n that have 4 as a digit
- Count numbers having 0 as a digit
- Count n digit numbers divisible by given number
- Count of n digit numbers whose sum of digits equals to given sum
- Count numbers formed by given two digit with sum having given digits
- Count numbers with unit digit k in given range
- Count of Numbers in a Range where digit d occurs exactly K times
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Count numbers with difference between number and its digit sum greater than specific value
- Count numbers have all 1s together in binary representation
- Count 'd' digit positive integers with 0 as a digit