Largest Binary Gap — Codility

Question:

int solution(int N);

Solution:

def solution(N):     
binStr = str(bin(N))[2:] # O(1)
zeroCounter= False
maxZeroCount = 0
zeroCounter = 0
for char in binStr: # O(N)
if char == '1':
if maxZeroCount < zeroCounter:
maxZeroCount = zeroCounter
zeroCounter = True
zeroCounter= 0
elif zeroCounter:
zeroCounter += 1
return maxZeroCount

Alternative Solution

def solution(N):
return len(max(bin(N)[2:].strip('0').strip('1').split('1')))

Time Complexity

Aspiring Software Engineer | Product Enthusiast https://www.linkedin.com/in/daryllwong/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store