File tree Expand file tree Collapse file tree 1 file changed +29
-0
lines changed
src/_DataStructures_/Queue/generate-binary-number Expand file tree Collapse file tree 1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change
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 ;
You can’t perform that action at this time.
0 commit comments