Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Generate Binary Numbers
EASY
10 mins
28 upvotes
Arrays
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Arrays
-
-
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Generate Binary Numbers

Easy
yellow-spark
0/40
Avg time to solve 10 mins
Success Rate 90 %
Share
28 upvotes

Problem Statement

Your friend Ninja has been learning about binary numbers lately. In order to understand binary numbers with perfection, Ninja asks you to generate a list of binary numbers from 1 to ‘N’, which he can use later for reference.

For every integer Ninja gives, your task is to generate all the binary numbers from 1 to ‘N’.

Example:

Consider N = 5,
All the binary numbers from 1 to 5 are: 1, 10, 11, 100, 101.
Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input 1:
2
2
6
Sample Output 1:
1 10
1 10 11 100 101 110
Explanation 1:
For the first test case when N = 2. 
We need all the binary numbers from 1 to 2:
1 -> 1
2 -> 10
Thus, the output is 1, 10.

For the second test case when N = 6
We need all the binary numbers from 1 to 6:
1 -> 1
2 -> 10
3 -> 11
4 -> 100
5 -> 101
6 -> 110
Thus, the output is 1, 10, 11, 100, 101, 110.
Sample Input 2:
2
8
4
Sample Output 2:
1 10 11 100 101 110 111 1000
1 10 11 100
Reset Code
Full screen
Auto
copy-code
Console