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.
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:
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course