-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day_85.cpp
60 lines (45 loc) · 1 KB
/
Day_85.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
DAY 85: Generate Binary Numbers.
https://www.geeksforgeeks.org/interesting-method-generate-binary-numbers-1-n/
QUESTION : Given a number N. The task is to generate and print all binary numbers with
decimal values from 1 to N.
Example 1:
Input:
N = 2
Output:
1 10
Explanation:
Binary numbers from
1 to 2 are 1 and 10.
Example 2:
Input:
N = 5
Output:
1 10 11 100 101
Explanation:
Binary numbers from
1 to 5 are 1 , 10 , 11 , 100 and 101.
Expected Time Complexity : O(N log2N)
Expected Auixilliary Space : O(N log2N)
Constraints:
1 ≤ N ≤ 10^6
*/
vector<string> generate(int N)
{
queue <string> q;
vector <string> numbers;
q.push("1");
while(N--) {
string top = q.front();
q.pop();
numbers.push_back(top);
string n1, n2;
n1 = top;
n2 = n1;
n1.append("0");
n2.append("1");
q.push(n1);
q.push(n2);
}
return numbers;
}