- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Check if any Pair of Consecutive 1s can be Separated by at most M 0s by Circular Rotation of a Binary String

Checking if any pair of consecutive 1s can be separated by at most M 0s by circular rotation of a Binary String is a common problem in computer programming and binary manipulation. The task is to determine whether a given binary string can be rotated in a circular manner such that any pair of consecutive 1s in the string can be separated by at most M 0s. This problem arises in various applications, such as image processing, data compression, and information retrieval.

In this tutorial, we will delve into the intricacies of this problem statement and provide a solution using C++. We will discuss the algorithmic approach and implementation details to efficiently solve this problem, along with the necessary code snippets and examples to illustrate the solution. So, let's dive in and starts learning something new!

## Problem Statement

Given a binary string of length N and an integer M, the task is to check if the binary string can be circularly rotated in a way that any pair of consecutive 1s in the string can be separated by at most M 0s.

### Sample Examples

**Example 1**

Input: Binary string: "100101", M = 2 Output: Yes

**Explanation:** In the given input, the binary string is "100101" and the value of M is 2. If we circularly rotate the string by 2 positions to the right, we get "011001". Now, any pair of consecutive 1s, i.e., "11", are separated by at most 2 0s, which satisfies the given condition. Hence, the output is "Yes".

**Example 2**

Input: Binary string: "110011", M = 1 Output: No

**Explanation:** In the given input, the binary string is "110011" and the value of M is 1. If we circularly rotate the string by any number of positions, we won't be able to separate the consecutive 1s "11" by at most 1 0, as there are two 0s between them. Hence, the output is "No".

### Algorithm

STEP 1: Read the binary string and the value of M from the input.

STEP 2: Initialize a counter variable to keep track of the number of consecutive 1s encountered.

STEP 3: Loop through the binary string from left to right.

STEP4: If the current character is '1', increment the counter.

STEP 5: If the current character is '0', check if the counter is greater than M. If yes, return "No" as the consecutive 1s cannot be separated by at most M 0s.

STEP 6: Continue the loop until the end of the binary string.

STEP 7: After the loop, check if the counter is greater than M. If yes, return "No" as the consecutive 1s cannot be separated by at most M 0s in the circular rotation.

STEP 8: Otherwise, return "Yes" as the consecutive 1s can be separated by at most M 0s in the circular rotation.

**Note:** In steps 5 and 7, the condition "counter > M" is used to check if the consecutive 1s can be separated by at most M 0s, as the counter represents the number of consecutive 1s encountered.

Now let’s understand the implementation of the above algorithm using C++ with an example.

### Example

**Implementation of the above algorithm using C++**

In the below program, we have defined a function 'checkConsecutiveOnes' that takes a binary string and an integer M as input and returns "Yes" if consecutive 1s can be separated by at most M 0s, and "No" otherwise. We then tested the function with two example inputs and displayed the output within the program itself.

#include <iostream> #include <string> using namespace std; // Function to check if consecutive 1s can be separated by at most M 0s string checkConsecutiveOnes(string binaryStr, int M) { int counter = 0; // Counter for consecutive 1s // Loop through the binary string for (int i = 0; i < binaryStr.length(); i++) { if (binaryStr[i] == '1') { counter++; // Increment counter for consecutive 1s } else { if (counter > M) { return "No"; // Consecutive 1s cannot be separated by at most M 0s } counter = 0; // Reset counter for consecutive 1s } } if (counter > M) { return "No"; // Consecutive 1s cannot be separated by at most M 0s } return "Yes"; // Consecutive 1s can be separated by at most M 0s } int main() { // Test Example 1 string binaryStr1 = "100101"; int M1 = 2; cout << "Binary String: " << binaryStr1 << ", M: " << M1 << ", Output: " << checkConsecutiveOnes(binaryStr1, M1) << endl; // Test Example 2 string binaryStr2 = "110011"; int M2 = 1; cout << "Binary String: " << binaryStr2 << ", M: " << M2 << ", Output: " << checkConsecutiveOnes(binaryStr2, M2) << endl; return 0; }

### Output

Binary String: 100101, M: 2, Output: Yes Binary String: 110011, M: 1, Output: No

## Conclusion

To sum up, the problem of checking if any pair of consecutive 1s can be separated by at most M 0s by circular rotation of a binary string can be efficiently solved using the algorithm and C++ program provided in this tutorial. By carefully considering the consecutive 1s and the maximum allowed 0s, we can determine whether the given condition is met or not. This solution can be useful in various applications where circular rotations of binary strings need to be analyzed, such as in pattern matching or data compression algorithms.