-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_gap.rs
More file actions
34 lines (29 loc) · 785 Bytes
/
binary_gap.rs
File metadata and controls
34 lines (29 loc) · 785 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
pub fn solution(num: i32) -> i32 {
let number_of_bits = (num as f32 + 1.).log2().ceil();
let mut found_one = false;
let mut maximum_gap = 0;
let mut possible_gap = 0;
for i in 0..number_of_bits as i32 {
if found_one && (num & (1 << i)) == 0 {
possible_gap += 1;
} else if num & (1 << i) != 0 {
if found_one {
if possible_gap > maximum_gap {
maximum_gap = possible_gap;
}
possible_gap = 0;
}
found_one = true;
}
}
maximum_gap
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_binary_gap() {
assert_eq!(solution(0b001010000100), 4);
assert_eq!(solution(0b000000000), 0);
}
}