Skip to content

Instantly share code, notes, and snippets.

@iamshadmirza
Created December 17, 2019 08:48
Show Gist options
  • Save iamshadmirza/4fc29a8b9ab16f4e92dcf064db07205e to your computer and use it in GitHub Desktop.
Save iamshadmirza/4fc29a8b9ab16f4e92dcf064db07205e to your computer and use it in GitHub Desktop.
binaryQueue solution from Cassidy's newsletter
// This week's question:
// Given a positive number N, generate binary numbers between 1 to N
// using a queue-type structure in linear time.
// Example:
// > binaryQueue(10)
// > 1 10 11 100 101 110 111 1000 1001 1010
function binaryQueue(n) {
const queue = [];
const result = [];
queue.unshift("1");
while(n > 0){
const current = queue.pop();
result.push(current);
queue.unshift(current + "1", current + "0");
n--;
}
return result;
}
console.log(binaryQueue(10));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment