Solution Generate Binary Numbers from 1 to N
Problem Statement
Given an integer N, generate all binary numbers from 1 to N and return them as a list of strings.
Examples
Example 1
- Input: N = 2
- Output: ["1", "10"]
- Explanation: The binary representation of 1 is "1", and the binary representation of 2 is "10".
Example 2
- Input: N = 3
- Output: ["1", "10", "11"]
- Explanation: The binary representation of 1 is "1", the binary representation of 2 is "10", and the binary representation of 3 is "11".
Example 3
- Input: N = 5
- Output: ["1", "10", "11", "100", "101"]
- Explanation: These are the binary representations of the numbers from 1 to 5.
Solution
To solve this problem, we'll use a queue to systematically generate binary numbers. Initially, we enqueue the binary representation of '1'. For each number from 1 to N, we perform the following steps: dequeue the front element of the queue and record it as the current binary number. Then, we generate the next two binary numbers by appending '0' and '1' to the current number and enqueue these new numbers. This process continues until we have generated all binary numbers up to N. This method efficiently leverages the queue's FIFO (First In First Out) nature to build upon previous binary numbers, ensuring a systematic and orderly generation of binary representations.
Step-by-Step Algorithm
-
Initialize a Queue: Create a queue data structure which will be used to hold binary numbers in string format.
-
Start with '1': Enqueue the binary representation of the first number, which is '1'.
-
Iterate up to N: Set up a loop that runs from 1 to N. This loop controls how many binary numbers you need to generate.
-
Dequeue and Output: In each iteration of the loop, dequeue an element from the front of the queue. This element is the binary representation of the current number. Store or print this number as part of the solution.
-
Generate Next Binary Numbers:
- Take the dequeued binary number and append '0' to it, forming the next binary number. Enqueue this new number back into the queue.
- Repeat the above step, but this time append '1' instead of '0'.
-
Repeat the Process: Continue this process until the loop completes its iteration up to N. Each iteration generates the next set of binary numbers based on the current numbers in the queue.
Algorithm Walkthrough

Code
Here is how we can implement this algorithm:
import java.util.LinkedList;
import java.util.Queue;
public class Solution {
public String[] generateBinaryNumbers(int n) {
Queue<String> q = new LinkedList<String>();
q.add("1"); // Initialize the queue with "1".
String[] res = new String[n]; // Create an array to store the binary numbers.
for (int i = 0; i < n; i++) {
res[i] = q.poll(); // Dequeue the current binary number and add it to the result array.
q.add(res[i] + "0"); // Enqueue the next binary number by appending "0".
q.add(res[i] + "1"); // Enqueue the next binary number by appending "1".
}
return res; // Return the array containing binary numbers.
}
public static void main(String[] args) {
Solution sol = new Solution();
String[] binaryNums = sol.generateBinaryNumbers(5); // Generate binary numbers for testing.
for (String binaryNum : binaryNums) {
System.out.println(binaryNum); // Print each generated binary number.
}
}
}
Complexity Analysis
Time Complexity
- Queue operations: The algorithm uses a queue to generate binary numbers in order. For each of the
nnumbers, it performs the following operations:- Dequeue operation: This is done once per number and takes
time. - Enqueue two new numbers: For each number, two new binary numbers (appending '0' and '1') are generated and enqueued. Each enqueue operation also takes
time.
- Dequeue operation: This is done once per number and takes
- Since there are
niterations, and each iteration involves constant-time operations for dequeueing and enqueueing, the overall time complexity is.
Overall time complexity:
Space Complexity
-
Result array: The result array stores the
nbinary numbers, which takesspace. -
Queue space: The queue stores intermediate binary numbers. At any point in time, the queue holds at most two binary numbers for each element processed. This means that the maximum number of elements in the queue is proportional to
n, so the queue requiresspace.
Overall space complexity:
🤖 Don't fully get this? Learn it with Claude
Stuck on Solution Generate Binary Numbers from 1 to N? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Progressively stronger hints — you still solve it.
I'm working on the problem **Solution Generate Binary Numbers from 1 to N** (DSA). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
See the technique, not just code.
Explain the optimal approach to **Solution Generate Binary Numbers from 1 to N** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Catch bugs, edge cases, sub-optimality.
I'll paste my solution to **Solution Generate Binary Numbers from 1 to N**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Lock in recognition with look-alikes.
Give me 2 problems that use the SAME underlying pattern as **Solution Generate Binary Numbers from 1 to N**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.