Skip to content

Commit 125db8e

Browse files
authored
Create binary_gap.rb
1 parent d9e226f commit 125db8e

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

binary_gap.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def solution(n)
2+
max_gap = 0
3+
temp_gap = 0
4+
counting = false
5+
num = n.to_s(2)
6+
num.split("").each do |el|
7+
8+
if el == "1"
9+
if counting
10+
max_gap = temp_gap if temp_gap > max_gap
11+
temp_gap = 0
12+
else
13+
counting = true
14+
end
15+
else
16+
if counting
17+
temp_gap += 1
18+
end
19+
end
20+
end
21+
max_gap
22+
end

0 commit comments

Comments
 (0)