Python | Check if there are K consecutive 1s in a binary number

Python Methods and Functions


 Input: binary number = 101010101111 k = 4 Output: yes Explanation: at the last 4 index there exists 4 consecutive 1's Input: binary number = 11100000 k = 5 Output: no Explanation: There is a maximum of 3 consecutive 1's in the given binary. 

Approach: create a new line with k 1 . Using the if condition, check if s is new. In python, if new in s: checks if any existence exists, if new in s, so it returns true, if there is, then returns false.

Below is the implementation of the above approach in Python:

# Python program to check if there is
# is k consecutive 1's in binary

# function to check if there is k
# consecutive 1

def check (s, k):


# generate a new line from k 1

new = "1" *


# if there is k 1 in any position

if new in s:

print " yes " 

  else :

print "no"  

# driver code

s = "10101001111"

k = 4  
check (s, k)