Skip to content

Instantly share code, notes, and snippets.

@alexislagante
Last active November 5, 2015 15:54
Show Gist options
  • Save alexislagante/07a8881824455f90cb4d to your computer and use it in GitHub Desktop.
Save alexislagante/07a8881824455f90cb4d to your computer and use it in GitHub Desktop.
Get Missing Numbers
// only support up to the n = 32
// assuming Math.log() is O(1)
function compute(array, max){
max = max || 32;
var sum = Math.pow(2,max)-1;
array.forEach(function(v){
sum -= Math.pow(2,v-1);
});
var result = [];
while (sum > 0) {
var y = Math.ceil(Math.log(sum+1)/Math.log(2));
sum = sum - Math.pow(2,y-1);
result.push(y);
}
return result;
}
compute([1,2,4,5,8,9,10],15);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment