# LeetCode 868Binary Gap

## LeetCode 868 Binary Gap Problem

``````class Solution:

# def binaryGap(self, n: int) -> int:
#     # Store index
#     A = [i for i in xrange(32) if (N >> i) & 1]
#     if len(A) < 2: return 0
#     return max(A[i+1] - A[i] for i in xrange(len(A) - 1))

def binaryGap(self, n: int) -> int:
# one pass and store max
current = 1
last1 = -1
out = 0
while n > 0:
if n % 2 == 1:
if last1 >= 1:
out = max(out, current - last1)
last1 = current
current += 1
n = n // 2
return out

# def binaryGap(self, n: int) -> int:
#     # one pass and store max
#     res = 0
#     last = -1
#     # Get binary encoding with bin
#     for i, curr in enumerate(bin(n)[2:]):
#         if curr == '1':
#             if last >= 0:
#                 res = max(res, i - last)
#             last = i
#     return res
``````

## List of all Binary Gap problems

Leetcode 868 Binary Gap problem solution in python3 with explanation. This is the best place to expand your knowledge and get prepared for your next interview.

Feedback is the most important part of any website.