Skip to content

Commit b2b1190

Browse files
committed
Added 1-n binary generator
1 parent 83b7de0 commit b2b1190

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

Diff for: README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 143 |
10-
| Current Streak | 7 days |
9+
| Total Problems | 144 |
10+
| Current Streak | 8 days |
1111
| Longest Streak | 91 ( August 17, 2015 - November 15, 2015 ) |
1212

1313
</center>
@@ -144,6 +144,7 @@ Include contains single header implementation of data structures and some algori
144144
| You are given an integer N. Find the digits in this number that exactly divide N (division that leaves 0 as remainder) and display their count. For N=24, there are 2 digits (2 & 4). Both of these digits exactly divide 24. So our answer is 2. See more details in header comment of the solution file. | [findDigits.cpp](common_ds_algo_problems/findDigits.cpp)|
145145
| Encrypt and then decrypts a text using Caeser Cipher. | [caeser_cipher.cpp](common_ds_algo_problems/caeser_cipher.cpp)|
146146
| Encrypt and then decrypts a text using Vigenère cipher. | [vigenere_cipher.cpp](common_ds_algo_problems/vigenere_cipher.cpp)|
147+
| Generate binary numbers between 1 to N efficiently. | [n_binary.cpp](common_ds_algo_problems/n_binary.cpp)|
147148

148149
### Math Problems| Problem | Solution |
149150
| :------------ | :----------: |

Diff for: common_ds_algo_problems/n_binary.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Generate numbers between 1 to N, in binary using a data-structure.
3+
*/
4+
5+
#include <iostream>
6+
#include <string>
7+
#include <queue>
8+
9+
void print_n_binary(int n)
10+
{
11+
std::queue<std::string> q;
12+
q.push("1");
13+
int i = 1;
14+
while (i++ <= n)
15+
{
16+
// append 0 and 1 to the existing binary string.
17+
//
18+
q.push(q.front() + "0");
19+
q.push(q.front() + "1");
20+
21+
std::cout << q.front() << std::endl;
22+
q.pop();
23+
}
24+
}
25+
26+
int main()
27+
{
28+
int n;
29+
std::cout << "Enter number n:";
30+
std::cin >> n;
31+
print_n_binary(n);
32+
return 0;
33+
}

0 commit comments

Comments
 (0)