Check if any circular rotation of String has at most X 1s between two adjacent 0s
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
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 existsvoid 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 Codeint 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 approachimport 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) { ArrayList<Integer> pos = new ArrayList<Integer>(); for (int i = 0; i < n; i++) { if (s.charAt(i) == '0') pos.add(i); } int cnt = 0; for (int i = 0; i + 1 < pos.size(); i++) { if ((pos.get(i + 1) - pos.get(i) - 1) > x) cnt++; } if (!pos.isEmpty() && (n - pos.get(pos.size() - 1) - 1 + pos.get(0) > x)) cnt++; if (cnt <= 1) System.out.println("Yes"); else System.out.println("No"); } // 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 existsdef 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 Codeif __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> |
Yes
Time Complexity: O(N)
Auxiliary Space: O(N)


