183. Generate Binary Numbers
Core1000 ms256 MBSolved by 0%
Print the binary representations of 1 through n, in order, without
converting any number to binary.
Instead, build each one from an earlier one. Starting from 1, appending a 0 and
appending a 1 produces the next two binary numbers in sequence — and a queue hands them
back in exactly the right order.
This is breadth-first generation, and it is the same shape as the graph search at the end of this topic.
Constraints - `1 ≤ n ≤ 100000`
Input
A single line containing one integer n.
Output
Print n binary strings on ONE line, separated by single spaces — the representations of 1, 2, ..., n in order, with no leading zeros.
Input5
Output1 10 11 100 101
Noteis the worked example from the statement
Input1
Output1
Noteis the smallest input, printing only the starting value
Hint 1Approach
Hint 2Approach
Hint 3Pseudocode
Hint 4Full solution