25. Generate Binary Numbers
โ GFG solution to the Generate Binary Numbers problem: generate binary representation of numbers from 1 to n using efficient conversion techniques. ๐
The problem can be found at the following link: ๐ Question Link
๐งฉ Problem Description
Given a number n
, the task is to generate all binary numbers with decimal values from 1
to n
.
A binary number is the base-2 representation of a decimal number, using only digits 0 and 1. The goal is to convert each decimal number from 1 to n into its corresponding binary string representation.
๐ Examples
Example 1
Input: n = 4
Output: ["1", "10", "11", "100"]
Explanation: Binary numbers from 1 to 4 are 1, 10, 11 and 100.
Example 2
Input: n = 6
Output: ["1", "10", "11", "100", "101", "110"]
Explanation: Binary numbers from 1 to 6 are 1, 10, 11, 100, 101 and 110.
๐ Constraints
$1 \le n \le 10^6$
โ
My Approach
The optimal approach uses Direct Decimal to Binary Conversion technique:
Direct Conversion Method
Initialize Result Array:
Create a vector/array to store binary strings for numbers 1 to n.
For Each Number i (1 to n):
Convert decimal number i to its binary representation.
Use division by 2 method: repeatedly divide by 2 and collect remainders.
Build binary string by prepending each remainder to the result.
Binary Conversion Process:
Start with number i and empty string.
While number > 0: get remainder (i % 2), prepend to string, divide i by 2.
Continue until number becomes 0.
Store and Continue:
Add the generated binary string to result array.
Process next number until all n numbers are converted.
๐ Time and Auxiliary Space Complexity
Expected Time Complexity: O(n * log n), where n is the input number. For each number i from 1 to n, we perform O(log i) operations to convert it to binary, resulting in O(n * log n) total operations.
Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space for temporary variables during conversion (excluding the output array space).
๐ง Code (C)
char** generate(int n) {
char** res = malloc(n * sizeof(char*));
for (int i = 0; i < n; i++) {
int num = i + 1, len = 0, temp = num;
while (temp) { len++; temp /= 2; }
res[i] = malloc((len + 1) * sizeof(char));
res[i][len] = '\0';
for (int j = len - 1; j >= 0; j--) {
res[i][j] = '0' + (num & 1);
num >>= 1;
}
}
return res;
}
๐งโ๐ป Code (C++)
class Solution {
public:
vector<string> generateBinary(int n) {
vector<string> res(n);
for (int i = 0; i < n; i++) {
string s = "";
for (int x = i + 1; x; x /= 2) s = char('0' + x % 2) + s;
res[i] = s;
}
return res;
}
};
โ Code (Java)
class Solution {
public ArrayList<String> generateBinary(int n) {
ArrayList<String> res = new ArrayList<>(n);
for (int i = 1; i <= n; i++) {
StringBuilder sb = new StringBuilder();
for (int x = i; x > 0; x /= 2) sb.append(x % 2);
res.add(sb.reverse().toString());
}
return res;
}
}
๐ Code (Python)
class Solution:
def generateBinary(self, n):
return [bin(i)[2:] for i in range(1, n + 1)]
๐ง Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: ๐ฌ Any Questions?. Let's make this learning journey more collaborative!
โญ If you find this helpful, please give this repository a star! โญ
๐Visitor Count
Last updated