Check if any circular rotation of String has at most X 1s between two adjacent 0s
Last Updated :
14 Feb, 2023
Given a binary string S of length N and an integer X, the task is to check if there exists a right wise circular rotation of the string such that every 2 adjacent 0?s are separated by at most X 1?s.
Note: The first and the last 0s in the string are not considered to be adjacent
Examples:
Input: S = "010110", X = 1
Output: Yes
?Explanation: The 6 circular rotations of the string
S = {"010110", "001011", "100101", "110010", "011001", "101100"}
out of which second , third and fourth strings satisfy the criteria.
Hence there exist a binary string which satisfy above condition.
Input: S = "0101", X = 0
?Output: No
An efficient approach: (No Addition of Auxiliary Space)
The input string could be thought of as a circle once we connect the last of the string to the first of it. The idea is to find out the first position of zero where the consecutive 1s between this element and the previous zero element is more than X. Next, if we need to rotate the string from that position to the left such that rest of elements in the string follows the constraints i.e. no of 1s between each pair of adjacent 0 is at most X. If we could not find any such arrangement after performing multiple rotation, then the Answer is No, else Yes. Only checking the rotation for the first incorrect position is sufficient because, there exists no right-wise circular rotation of the string such that every 2 adjacent 0?s are separated by at most X 1?s, if there is multiple positions where the no of consecutive 1s between two consecutive 0s does not follow the strategy. This would been possible if we can rotate both clock wise (right rotation) and anti-clock wise (left rotation).
Follow the below steps to implement the above idea:
- Find out the first position of zero where it violates the rule (no of consecutive 1 between each pair of 0 is at most X), also track the first 0th position (it will help if any such position exists where this given rule is not satisfied)
- If any such position of zero is found, count the no of 1s from the next position and move circularly to the first of the string until the first 0th position).
- If no of consecutive 1 found is more than X, the Answer is NO, else YES
Below is the implementation of the above approach.
C++
#include <iostream>
#include <string>
// Function to check if sequence exist
bool checkIfSequenceExist(std::string str, int x)
{
int i = 0;
int previous_zero_position = -1;
int no_of_consequtive_1 = 0;
int incorrect_oth_position = -1;
int first_oth_position = -1;
bool exist = true;
// iterate through the string
while (i < str.length()) {
if (str[i]
== '0') { // if the element is 0, check if any
// previous element=0 found
if (previous_zero_position != -1) {
if (no_of_consequtive_1 <= x) {
no_of_consequtive_1
= 0; // valid condition
}
else {
incorrect_oth_position
= i; // violates the constraint
break;
}
}
else {
first_oth_position = i;
}
previous_zero_position
= i; // update previous_zero_position to the
// latest 0th position
}
else if (first_oth_position
!= -1) { // no need to track no of 1s if
// there is no element=0 found so
// far
no_of_consequtive_1 += 1;
}
i += 1;
}
if (incorrect_oth_position != -1) {
no_of_consequtive_1 = 0;
i = (incorrect_oth_position + 1) % str.length();
while (i != first_oth_position) {
i = (i + 1) % str.length();
no_of_consequtive_1 += 1;
}
if (no_of_consequtive_1 > x) {
exist = false;
}
}
return exist;
}
int main()
{
std::string str = "010110";
int x = 1;
bool exist = checkIfSequenceExist(str, x);
if (exist) {
std::cout << "Yes" << std::endl;
}
else {
std::cout << "No" << std::endl;
}
return 0;
}
// This code is contributed by shivamsharma215
Java
import java.io.*;
public class GFG {
public static void main(String[] args)
{
String str = "010110";
int x = 1;
boolean exist = checkIfSequeneceExist(str, x);
if (exist)
System.out.println("Yes");
else
System.out.println("No");
}
private static boolean checkIfSequeneceExist(String str,
int x)
{
int i = 0, previousZeroPosition = -1,
noOfConsequtive1 = 0, incorrectOthPosition = -1,
firstOthPosition = -1;
boolean exist = true;
while (i < str.length()) {
if (str.charAt(i)
== '0') { // if the element is 0, check if
// any previous element=0 found
if (previousZeroPosition != -1) {
if (noOfConsequtive1 <= x)
noOfConsequtive1
= 0; // valid condition
else {
incorrectOthPosition
= i; // violates the constraint
break;
}
}
else {
firstOthPosition = i;
}
previousZeroPosition
= i; // update previousZeroPosition to
// the latest 0th position
}
else if (firstOthPosition
!= -1) // no need to track no of 1s if
// there is no element=0 found
// so far
noOfConsequtive1++;
i++;
}
if (incorrectOthPosition != -1) {
noOfConsequtive1 = 0;
i = (incorrectOthPosition + 1) % str.length();
while (i != firstOthPosition) {
i = (i + 1) % str.length();
noOfConsequtive1++;
}
if (noOfConsequtive1 > x)
exist = false;
}
return exist;
}
}
Python3
def checkIfSequenceExist(str, x):
i = 0
previous_zero_position = -1
no_of_consequtive_1 = 0
incorrect_oth_position = -1
first_oth_position = -1
exist = True
# iterate through the string
while i < len(str):
if str[i] == '0': # if the element is 0, check if any previous element=0 found
if previous_zero_position != -1:
if no_of_consequtive_1 <= x:
no_of_consequtive_1 = 0 # valid condition
else:
incorrect_oth_position = i # violates the constraint
break
else:
first_oth_position = i
previous_zero_position = i # update previous_zero_position to the latest 0th position
elif first_oth_position != -1: # no need to track no of 1s if there is no element=0 found so far
no_of_consequtive_1 += 1
i += 1
if incorrect_oth_position != -1:
no_of_consequtive_1 = 0
i = (incorrect_oth_position + 1) % len(str)
while i != first_oth_position:
i = (i + 1) % len(str)
no_of_consequtive_1 += 1
if no_of_consequtive_1 > x:
exist = False
return exist
# Driver code
str = "010110"
x = 1
exist = checkIfSequenceExist(str, x)
if exist:
print("Yes")
else:
print("No")
C#
// C# code for the above approach
using System;
public class GFG {
static bool checkIfSequeneceExist(string str, int x)
{
int i = 0, previousZeroPosition = -1,
noOfConsequtive1 = 0, incorrectOthPosition = -1,
firstOthPosition = -1;
bool exist = true;
while (i < str.Length) {
if (str[i]
== '0') { // if the element is 0, check if
// any previous element=0 found
if (previousZeroPosition != -1) {
if (noOfConsequtive1 <= x)
noOfConsequtive1
= 0; // valid condition
else {
incorrectOthPosition
= i; // violates the constraint
break;
}
}
else {
firstOthPosition = i;
}
previousZeroPosition
= i; // update previousZeroPosition to
// the latest 0th position
}
else if (firstOthPosition
!= -1) // no need to track no of 1s if
// there is no element=0 found
// so far
noOfConsequtive1++;
i++;
}
if (incorrectOthPosition != -1) {
noOfConsequtive1 = 0;
i = (incorrectOthPosition + 1) % str.Length;
while (i != firstOthPosition) {
i = (i + 1) % str.Length;
noOfConsequtive1++;
}
if (noOfConsequtive1 > x)
exist = false;
}
return exist;
}
static public void Main()
{
// Code
string str = "010110";
int x = 1;
bool exist = checkIfSequeneceExist(str, x);
if (exist)
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by lokeshmvs21.
JavaScript
function checkIfSequeneceExist(str, x)
{
let i = 0, previousZeroPosition = -1,
noOfConsequtive1 = 0, incorrectOthPosition = -1,
firstOthPosition = -1;
let exist = true;
while (i < str.length) {
if (str.charCodeAt(i)
== '0') { // if the element is 0, check if
// any previous element=0 found
if (previousZeroPosition != -1) {
if (noOfConsequtive1 <= x)
noOfConsequtive1
= 0; // valid condition
else {
incorrectOthPosition
= i; // violates the constraint
break;
}
}
else {
firstOthPosition = i;
}
previousZeroPosition
= i; // update previousZeroPosition to
// the latest 0th position
}
else if (firstOthPosition
!= -1) // no need to track no of 1s if
// there is no element=0 found
// so far
noOfConsequtive1++;
i++;
}
if (incorrectOthPosition != -1) {
noOfConsequtive1 = 0;
i = (incorrectOthPosition + 1) % str.length;
while (i != firstOthPosition) {
i = (i + 1) % str.length;
noOfConsequtive1++;
}
if (noOfConsequtive1 > x)
exist = false;
}
return exist;
}
let str = "010110";
let x = 1;
let exist = checkIfSequeneceExist(str, x);
if (exist)
document.write("Yes");
else
document.write("No");
Time Complexity: O(N)
Auxiliary Space: O(1)
Approach: The problem can be solved based on the following observation:
Assuming the last character to be adjacent to first, we can find the number of ones between each pair of adjacent ones in a list. Now, the rotation of binary string is equivalent to deleting at most one element of this list. So if rest of the elements are up to X, then the answer is YES.
Follow the below steps to implement the above idea:
- Find the positions of all 0's in the array.
- Find the number of 1s between any two adjacent 0s.
- If there is at most 1 such segment of contiguous 1s having length X or more then that segment can be partitioned to be in the start or last. So print "Yes";
- Otherwise, it prints "No".
Below is the implementation of the above approach.
C++
// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
// Function to check whether
// such a rotation exists
void check(string s, int n, int x)
{
vector<int> pos;
for (int i = 0; i < n; i++) {
if (s[i] == '0')
pos.push_back(i);
}
int cnt = 0;
for (int i = 0; i + 1 < pos.size(); i++) {
if ((pos[i + 1] - pos[i] - 1) > x)
cnt++;
}
if (!pos.empty() and n - pos.back() - 1 + pos[0] > x)
cnt++;
if (cnt <= 1)
cout << "Yes\n";
else
cout << "No\n";
}
// Driver Code
int main()
{
string S = "010110";
int N = S.length();
int X = 1;
// Function call
check(S, N, X);
return 0;
}
Java
// Java code to implement the approach
import java.io.*;
import java.util.*;
class GFG {
// Function to check whether
// such a rotation exists
public static void check(String s, int n, int x)
{
int i = 0, previousZeroPosition=-1, noOfConsequtive1 =0, incorrectOthPosition = -1, firstOthPosition = -1;
boolean exist=true;
while(i< n) {
if(s.charAt(i) == '0') {
if(previousZeroPosition != -1) {
if(noOfConsequtive1 <= x)
noOfConsequtive1 = 0;
else{
incorrectOthPosition = i;
break;
}
}else {
firstOthPosition = i;
}
previousZeroPosition = i;
}else if(firstOthPosition != -1) {
noOfConsequtive1++;
}
i++;
}
if(incorrectOthPosition != -1) {
noOfConsequtive1 = 0; i = (incorrectOthPosition + 1)%n;
while(i != firstOthPosition) {
i = (i + 1)%n;
noOfConsequtive1++;
}
if(noOfConsequtive1 > x)
exist = false;
}
System.out.println(exist);
}
// Driver Code
public static void main(String[] args)
{
String S = "010110";
int N = S.length();
int X = 1;
// Function call
check(S, N, X);
}
}
Python3
# Python code to implement the approach
# Function to check whether
# such a rotation exists
def check(s, n, x):
pos = []
for i in range(n):
if (s[i] == '0'):
pos.append(i)
cnt = 0
for i in range(len(pos)-1):
if ((pos[i + 1] - pos[i] - 1) > x):
cnt += 1
if (len(pos) != 0 and n - pos[len(pos)-1] - 1 + pos[0] > x):
cnt += 1
if (cnt <= 1):
print("Yes")
else:
print("No")
# Driver Code
if __name__ == "__main__":
S = "010110"
N = len(S)
X = 1
# Function call
check(S, N, X)
# This code is contributed by Rohit Pradhan
C#
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class GFG{
// Function to check whether
// such a rotation exists
public static void check(String s, int n, int x)
{
ArrayList pos = new ArrayList();
for (int i = 0; i < n; i++) {
if (s[i] == '0')
pos.Add(i);
}
int cnt = 0;
for (int i = 0; i + 1 < pos.size(); i++) {
if (((int)pos[i + 1] - (int)pos[i] - 1) > x)
cnt++;
}
if (!pos.Any()
&& (n - pos[pos.Count - 1] - 1 + pos[0]
> x))
cnt++;
if (cnt <= 1)
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
static public void Main (){
// Code
String S = "010110";
int N = S.Length;
int X = 1;
// Function call
check(S, N, X);
}
}
JavaScript
<script>
function check(s, n, x){
let pos = [];
for(let i = 0; i < n; i++){
if(s.charAt(i) == '0'){
pos.push(i);
}
}
var cnt = 0;
for(let i = 0; i + 1 < pos.length; i++){
if((pos[i + 1] - pos[i] - 1) > x){
cnt += 1;
}
}
if((pos.length != 0) && (n - pos[(pos.length) - 1] + pos[0] > x))
{
cnt += 1;
}
if(cnt <= 1){
document.write("Yes");
}
else{
document.write("No");
}
}
let S = "010110";
let N = S.length;
let X = 1;
// Function call
check(S, N, X);
// This code is contributed by lokeshmvs21.
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Check if any pair of consecutive 1s can be separated by at most M 0s by circular rotation of a Binary String
Given a binary string S of length N and a positive integer M, the task is to check if it is possible to rotate the string circularly any number of times such that any pair of consecutive 1s are separated by at most M 0s. If it is possible, then print "Yes". Otherwise, print "No". Examples: Input: S
8 min read
Check if a binary string has a 0 between 1s or not | Set 1 (General approach)
Given a string of 0 and 1, we need to check that the given string is valid or not. The given string is valid when there is no zero present in between 1's. For example, 1111, 0000111110, 1111000 are valid strings but 01010011, 01010, 101 are not. One approach to solving the problem is discussed here,
11 min read
Check if all rows of a matrix are circular rotations of each other
Given a matrix of n*n size, the task is to find whether all rows are circular rotations of each other or not. Examples: Input: mat[][] = 1, 2, 3 3, 1, 2 2, 3, 1 Output: Yes All rows are rotated permutation of each other. Input: mat[3][3] = 1, 2, 3 3, 2, 1 1, 3, 2 Output: No Explanation : As 3, 2, 1
8 min read
Check if two Binary Strings can be made equal by doing bitwise XOR of adjacent
Given binary strings S1 and S2 of length N, the task is to check if S2 can be made equal to S1 by performing the following operations on S2: The first operation is Si = Si ? Si+1. ( ? is the XOR operation)The second operation is Si+1 = Si+1 ? Si. Examples: Input: S1 = "00100", S2 = "00011" Output: Y
6 min read
Count of 0s to be flipped to make any two adjacent 1s at least K 0s apart
Given a binary string s and a number K, the task is to find the maximum number of 0s that can be replaced by 1s such that two adjacent 1s are separated by at least K 0s in between them. Examples: Input: K = 2, s = "000000" Output: 2 Explanation:Change the 0s at position 0 and 3. Then the final strin
8 min read
Check if it is possible to rearrange a binary string with alternate 0s and 1s
Given a binary string of length, at least two. We need to check if it is possible to rearrange a binary string such that there are alternate 0s and 1s. If possible, then the output is YES, otherwise the output is NO. Examples: Input : 1011 Output : NO We can't rearrange the string such that it has a
5 min read
Check if count of 1s can be made greater in a Binary string by changing 0s adjacent to 1s
Given a binary string S of size N, the task is to check if the count of 1s can be made greater than the count of 0s by changing the 0s adjacent to 1s to any other characters. If it is possible, then print Yes. Otherwise, print No. Note: Any index having 1 can be chosen at most once. Examples: Input:
9 min read
Check if a binary string has a 0 between 1s or not | Set 2 (Regular Expression Approach)
Given a string of 0 and 1, we need to check that the given string is valid or not. The given string is valid when there is no zero is present in between 1's. For example, 1111, 0000111110, 1111000 are valid strings but 01010011, 01010, 101 are not. Examples: Input : 100 Output : VALID Input : 111000
3 min read
Check if all substrings of length K of a Binary String has equal count of 0s and 1s
Given a binary string S of length N and an even integer K, the task is to check if all substrings of length K contains an equal number of 0s and 1s. If found to be true, print âYesâ. Otherwise, print âNoâ. Examples: Input: S = "101010", K = 2Output: YesExplanation:Since all the substrings of length
6 min read
Javascript Program to Check if a string can be formed from another string by at most X circular clockwise shifts
Given an integer X and two strings S1 and S2, the task is to check that string S1 can be converted to the string S2 by shifting characters circular clockwise atmost X times. Input: S1 = "abcd", S2 = "dddd", X = 3 Output: Yes Explanation: Given string S1 can be converted to string S2 as- Character "a
3 min read