-
Notifications
You must be signed in to change notification settings - Fork 0
/
binaryGap.js
53 lines (42 loc) · 1.6 KB
/
binaryGap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// global variable to keep the max gap size and use recursivity
var maxGap = 0;
function getGap(binarySplit){
//find fist 1
var start = binarySplit.indexOf('1');
//find next 1 based on where the first was found
var end = binarySplit.indexOf('1', start + 1);
//when end = -1 means that there's not other 1 to compare, a gap can be find and the search ends
if(end < 0){
return maxGap;
}
//calculate current gap size, based on the end index less start index plus one (arrays are cero based)
var gapSize = end - (start + 1);
//if the global maxGap is minor than the current gap size, the value has to be updated
if(maxGap < gapSize){
maxGap = gapSize;
}
//while the array size is max than the end index, is necessary to check next values
if(binarySplit.length > end){
// create a new array with pending values to analize
var newBinarySplit = binarySplit.slice(end, binarySplit.length);
getGap(newBinarySplit);
}
// return final value
return maxGap;
}
//dinamic max value to test but always has to be bigger than 1 because is a integer
function isValidRange(number, maxRange){
return number < 1 || number > maxRange;
}
function solution(number, maxRange) {
if(!Number.isInteger(number)){ // validate if is a number
return 0;
}
if(isValidRange(number, maxRange)){
return 0;
}
var binary = number.toString(2); // parse to binary
var binarySplit = binary.split(''); //split the binary to work with
//call recursive function
return getGap(binarySplit);
}