-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbinaryGap.js
More file actions
34 lines (27 loc) · 793 Bytes
/
binaryGap.js
File metadata and controls
34 lines (27 loc) · 793 Bytes
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
// -----------------------------------------------------------------------------
// BINARY GAP
// Find longest sequence of zeros in binary representation of an integer.
//
// Report: https://app.codility.com/demo/results/trainingR65Q82-ZJ2/
// -----------------------------------------------------------------------------
function solution(N) {
let n = N
let gap = 0
let max_gap = 0
let started = false
while (n > 0) {
const digit = n % 2
n = Math.floor(n / 2)
if (digit == 1) {
gap = 0
started = true
} else if (started) {
gap += 1
}
max_gap = Math.max(max_gap, gap)
}
return max_gap
}
console.log(solution(1041));
console.log(solution(15));
console.log(solution(32));