We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2dc60ee commit 0014f40Copy full SHA for 0014f40
alternative/easy/binary_gab.py
@@ -0,0 +1,24 @@
1
+def find_binary_gap(n):
2
+ max_gap = 0
3
+ current_gap = 0
4
+ start_counting = False
5
+ num = n
6
+ while num > 0:
7
+ if num & 1 == 1:
8
+ if not start_counting:
9
+ start_counting = True
10
+ else:
11
+ max_gap = max(max_gap, current_gap)
12
13
+ elif start_counting:
14
+ current_gap += 1
15
+ num >>= 1
16
+ return max_gap
17
+
18
+# Test cases
19
+assert find_binary_gap(9) == 2
20
+assert find_binary_gap(529) == 4
21
+assert find_binary_gap(20) == 1
22
+assert find_binary_gap(15) == 0
23
+assert find_binary_gap(32) == 0
24
+assert find_binary_gap(1041) == 5
0 commit comments