Last active
November 5, 2015 15:54
-
-
Save alexislagante/07a8881824455f90cb4d to your computer and use it in GitHub Desktop.
Get Missing Numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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