We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 61049dc commit a8dac97Copy full SHA for a8dac97
src/_DataStructures_/Queue/generate-binary-number/index.js
@@ -0,0 +1,29 @@
1
+const Queue = require('../index');
2
+
3
+function generateBinaryNumber(n) {
4
+ const result = [];
5
+ const q = new Queue();
6
7
+ // add `1` to the queue
8
+ q.enqueue('1');
9
10
+ // iterate till the given number
11
+ for (let i = 0; i < n; i += 1) {
12
+ // push the item in the queue to the array
13
+ result.push(q.dequeue());
14
15
+ // append `0` & `1` respectively
16
+ const s1 = `${result[i]}0`;
17
+ const s2 = `${result[i]}1`;
18
19
+ // push the combinations in the queue
20
+ q.enqueue(s1);
21
+ q.enqueue(s2);
22
+ }
23
+ // return the result containing all the binary numbers
24
+ return result;
25
+}
26
27
+// console.log(generateBinaryNumber(5));
28
29
+module.exports = generateBinaryNumber;
0 commit comments