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

Given K and a binary number, check if there exists k consecutive 1’s in the binary number.

Examples:

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 string with k 1’s. Using if condition check if there is new in s. In python if new in s: checks if there is any existence if new in s, hence returns true if there is else it returns a false.

Below is the Python implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to check if there 
# is k consecutive 1's in a binary number 
  
# fucntion to check if there are k 
# consecutive 1's 
def check(s,k):
      
    # form a new string of k 1's 
    new = "1"*
      
    # if there is k 1's at any position 
    if new in s:
        print "yes" 
    else:
        print "no" 
  
# driver code
s = "10101001111"
k = 4 
check(s, k)

chevron_right


Output:

yes


My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.