# 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:

## Python

 `# Python program to check if there ` `# is k consecutive 1's in a binary number `   `# function to check if there are k ` `# consecutive 1's ` `def` `check(s,k): ` `    `  `    ``# form a new string of k 1's ` `    ``new ``=` `"1"``*``k ` `    `  `    ``# 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)`

Output

`yes`

Time Complexity : O(N)
Auxiliary Space : O(1)

Approach : Using find() method

## Python3

 `# Python program to check if there` `# is k consecutive 1's in a binary number`   `s ``=` `"10101001111"` `k ``=` `4`   `res ``=` `"NO"`   `if``(s.find(``"1"``*``k) !``=` `-``1``):` `    ``res ``=` `"YES"`   `print``(res)`

Output

`YES`

Time complexity: O(n), where n is the length of s.
Auxiliary space: O(1), as we are only using a constant amount of memory for the string s, the integer k, and the string res.

Another approach to solve this problem is to use a sliding window approach. In this approach, we can check for the presence of k consecutive 1’s by considering a window of size k and sliding it through the entire binary number.

Here is an example of how this approach can be implemented in Python:

## Python3

 `def` `check_consecutive_ones(s, k):` `    ``for` `i ``in` `range``(``len``(s) ``-` `k ``+` `1``):` `        ``if` `all``(s[i ``+` `j] ``=``=` `"1"` `for` `j ``in` `range``(k)):` `            ``return` `True` `    ``return` `False`   `s ``=` `"10101001111"` `k ``=` `4`   `if` `check_consecutive_ones(s, k):` `    ``print``(``"Yes"``)` `else``:` `    ``print``(``"No"``)`

Output

`Yes`

This approach uses a for loop to iterate through each possible starting index of a sequence of k 1’s in the binary string and then checks if all the subsequent characters in the string at those indices are 1’s using the all() function. If any sequence of k 1’s is found, the function returns True, otherwise, it returns False.

The time complexity of the check_consecutive_ones() function is O(n*k), where n is the length of the input string. This is because the function involves iterating through all the characters in the string and checking if there is a consecutive subsequence of k ones.
The Auxiliary Space of the check_consecutive_ones() function is O(k), The all() function returns True if all the elements in an iterable are True, and False otherwise.

Approach : Using replace() and len() methods

## Python3

 `# Python program to check if there` `# is k consecutive 1's in a binary number`   `s ``=` `"10101001111"` `k ``=` `4` `a``=``"1"``*``k` `res ``=` `"NO"` `b``=``s.replace(a,"")` `if``(``len``(b)!``=``len``(s)):` `    ``res ``=` `"YES"` `print``(res)`

Output

`YES`

Time Complexity : O(N)
Auxiliary Space : O(N)

Approach : Using operator.contains() method

Check whether k consecutive 1’s are present in given binary string using operator.contains() method

## Python3

 `# Python program to check if there` `# is k consecutive 1's in a binary number`   `s ``=` `"10101001111"` `k ``=` `4` `a``=``"1"``*``k` `res ``=` `"NO"` `import` `operator` `if` `operator.contains(s,a):` `    ``res``=``"YES"` `print``(res)`

Output

`YES`

Time Complexity : O(N) where N is the length of s.

Auxiliary Space : O(1) as we are only using a constant amount of memory for the string s, the integer k, and the string res

Approach: Using  a regular expression

• Imports the re module
• Sets the input binary string and the number of consecutive 1’s to check using tuple unpacking
• Searches for a pattern of k consecutive 1’s in the binary string using a regular expression with the re.search() function
• Sets the result to “YES” if the pattern is found, and “NO” if it is not, using a ternary operator
• Prints the result to the console.

## Python3

 `import` `re`   `# Input binary string and number of consecutive 1's to check` `s ``=` `"10101001111"` `k ``=` `4`   `# Create regular expression pattern to match k consecutive 1's` `pattern ``=` `'1{'` `+` `str``(k) ``+` `'}'`   `# Initialize result to "NO"` `res ``=` `"NO"`   `# Search for pattern in binary string` `if` `re.search(pattern, s):` `   ``# If pattern is found, set result to "YES"` `   ``res ``=` `"YES"`   `# Output the result` `print``(res)`

Output

```YES
```

Time Complexity: O(N) where N is the length of s.

Auxiliary Space: O(N) because the regular expression pattern is created based on the input value of k,.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Previous
Next