Skip to content

Instantly share code, notes, and snippets.

@hritik5102
Last active May 19, 2023 12:34
Show Gist options
  • Save hritik5102/77f3dd4513d2b7da31029047f9a4c715 to your computer and use it in GitHub Desktop.
Save hritik5102/77f3dd4513d2b7da31029047f9a4c715 to your computer and use it in GitHub Desktop.
Cyclic binary string hackerrank solution
'''
Author : Hritik Jaiswal
Problem : https://www.hackerrank.com/contests/hackerrank-hackfest-2020/challenges/cyclic-binary-string
Date : October 17 2020
Reference : https://www.youtube.com/watch?v=h3B49vlhbEw
Challenge : Hackerrank HackFest 2020
'''
def maximumPower(s):
# Write your code here
n = len(s)
count,maxcount,zerocount= 0,0,0
for i in range(n):
if s[i]=='0':
count+=1
zerocount+=1
else:
maxcount = max(maxcount,count)
count=0
maxcount = max(maxcount,count)
if (zerocount==n):
return -1
k = 0
l = n-1
firstcount,lastcount=0,0
while(s[k]=='0'):
firstcount+=1
k+=1
while(s[l]=='0'):
lastcount+=1
l-=1
maxcount = max(maxcount, firstcount+lastcount)
return maxcount
if __name__ == '__main__':
s = input()
result = maximumPower(s)
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment