-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathBinaryGap.py
36 lines (26 loc) · 864 Bytes
/
BinaryGap.py
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
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
# write your code in Python 3.6
# using the "concept of bit manipulation" and "& operation"
current_gap = 0
max_gap = 0
start_counting = False
temp = N
while temp > 0:
# case 1
if (temp & 1 == 1):
# case 1-1
if (start_counting == False):
start_counting = True
# case 1-2
elif (start_counting == True):
max_gap = max(max_gap, current_gap)
current_gap = 0 #reset
# case 2
elif (temp & 1 == 0):
if(start_counting == True):
current_gap += 1
# shift one bit (every loop)
temp = temp >> 1
return max_gap