Maximum number of 0s placed consecutively at the start and end in any rotation of a Binary String
Last Updated :
26 Apr, 2023
Given a binary string S of size N, the task is to maximize the sum of the count of consecutive 0s present at the start and end of any of the rotations of the given string S.
Examples:
Input: S = “1001”
Output: 2
Explanation:
All possible rotations of the string are:
“1001”: Count of 0s at the start = 0; at the end = 0. Sum= 0 + 0 = 0.
“0011”: Count of 0s at the start = 2; at the end = 0. Sum = 2 + 0=2
“0110”: Count of 0s at the start = 1; at the end = 1. Sum= 1 + 1 = 2.
“1100”: Count of 0s at the start = 0; at the end = 2. Sum = 0 + 2 = 2
Therefore, the maximum sum possible is 2.
Input: S = “01010”
Output: 2
Explanation:
All possible rotations of the string are:
“01010”: Count of 0s at the start = 1; at the end = 1. Sum= 1+1=1
“10100”: Count of 0s at the start = 0; at the end = 2. Sum= 0+2=2
“01001”: Count of 0s at the start = 1; at the end = 0. Sum= 1+0=1
“10010”: Count of 0s at the start = 0; at the end = 1. Sum= 0+1=1
“00101”: Count of 0s at the start = 2; at the end = 0. Sum= 2+0=2
Therefore, the maximum sum possible is 2.
Naive Approach: The simplest idea is to generate all rotations of the given string and for each rotation, count the number of 0s present at the beginning and end of the string and calculate their sum. Finally, print the maximum sum obtained.
Algorithm:
- Initialize a counter variable c0 to 0 to count the frequency of 0s in the given string.
- Traverse the string and for each character, if it is 0, increment the value of c0 by 1.
- If the value of c0 is equal to the length of the string n, it means that all the characters in the string are 0, so the maximum sum of consecutive 0s present at the start and end of the string will be n. Print n and return.
- Concatenate the string with itself and store it in a new string s.
- Initialize a variable mx to 0 to store the maximum sum of consecutive 0s present at the start and end of a string in any rotation of the given string.
- Generate all rotations of the string s using a loop that iterates from 0 to n-1.
- For each rotation, initialize two variables cs and ce to 0 to store the number of consecutive 0s at the start and end of the string, respectively.
- Traverse the rotated string from the current index i to i+n-1, and for each character, if it is 0, increment the value of cs by 1, else break out of the loop.
- Traverse the rotated string from the current index i+n-1 to i, and for each character, if it is 0, increment the value of ce by 1, else break out of the loop.
- Calculate the sum of cs and ce and store it in a variable val.
- Update the value of mx to the maximum of its current value and val.
- After the loop ends, the value of mx will contain the maximum sum of consecutive 0s present at the start and end of a string in any rotation of the given string. Print mx.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findMaximumZeros(string str, int n)
{
int c0 = 0;
for ( int i = 0; i < n; ++i) {
if (str[i] == '0' )
c0++;
}
if (c0 == n) {
cout << n;
return ;
}
string s = str + str;
int mx = 0;
for ( int i = 0; i < n; ++i) {
int cs = 0;
int ce = 0;
for ( int j = i; j < i + n; ++j) {
if (s[j] == '0' )
cs++;
else
break ;
}
for ( int j = i + n - 1; j >= i; --j) {
if (s[j] == '0' )
ce++;
else
break ;
}
int val = cs + ce;
mx = max(val, mx);
}
cout << mx;
}
int main()
{
string s = "1001" ;
int n = s.size();
findMaximumZeros(s, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void findMaximumZeros(String str, int n)
{
int c0 = 0 ;
for ( int i = 0 ; i < n; ++i)
{
if (str.charAt(i) == '0' )
c0++;
}
if (c0 == n)
{
System.out.print(n);
return ;
}
String s = str + str;
int mx = 0 ;
for ( int i = 0 ; i < n; ++i)
{
int cs = 0 ;
int ce = 0 ;
for ( int j = i; j < i + n; ++j)
{
if (s.charAt(j) == '0' )
cs++;
else
break ;
}
for ( int j = i + n - 1 ; j >= i; --j)
{
if (s.charAt(j) == '0' )
ce++;
else
break ;
}
int val = cs + ce;
mx = Math.max(val, mx);
}
System.out.print(mx);
}
public static void main(String[] args)
{
String s = "1001" ;
int n = s.length();
findMaximumZeros(s, n);
}
}
|
Python3
def findMaximumZeros(st, n):
c0 = 0
for i in range (n):
if (st[i] = = '0' ):
c0 + = 1
if (c0 = = n):
print (n)
return
s = st + st
mx = 0
for i in range (n):
cs = 0
ce = 0
for j in range (i, i + n):
if (s[j] = = '0' ):
cs + = 1
else :
break
for j in range (i + n - 1 , i - 1 , - 1 ):
if (s[j] = = '0' ):
ce + = 1
else :
break
val = cs + ce
mx = max (val, mx)
print (mx)
if __name__ = = "__main__" :
s = "1001"
n = len (s)
findMaximumZeros(s, n)
|
C#
using System;
class GFG{
static void findMaximumZeros( string str, int n)
{
int c0 = 0;
for ( int i = 0; i < n; ++i)
{
if (str[i] == '0' )
c0++;
}
if (c0 == n)
{
Console.Write(n);
return ;
}
string s = str + str;
int mx = 0;
for ( int i = 0; i < n; ++i)
{
int cs = 0;
int ce = 0;
for ( int j = i; j < i + n; ++j)
{
if (s[j] == '0' )
cs++;
else
break ;
}
for ( int j = i + n - 1; j >= i; --j)
{
if (s[j] == '0' )
ce++;
else
break ;
}
int val = cs + ce;
mx = Math.Max(val, mx);
}
Console.Write(mx);
}
public static void Main( string [] args)
{
string s = "1001" ;
int n = s.Length;
findMaximumZeros(s, n);
}
}
|
Javascript
<script>
function findMaximumZeros(str, n)
{
var c0 = 0;
var i;
for (i = 0; i < n; ++i) {
if (str[i] == '0' )
c0++;
}
if (c0 == n) {
document.write(n);
return ;
}
var s = str + str;
var mx = 0;
var j;
for (i = 0; i < n; ++i) {
var cs = 0;
var ce = 0;
for (j = i; j < i + n; ++j) {
if (s[j] == '0' )
cs++;
else
break ;
}
for (j = i + n - 1; j >= i; --j) {
if (s[j] == '0' )
ce++;
else
break ;
}
var val = cs + ce;
mx = Math.max(val, mx);
}
document.write(mx);
}
var s = "1001" ;
var n = s.length;
findMaximumZeros(s, n);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
Efficient Approach: The idea is to find the maximum number of consecutive 0s in the given string. Also, find the sum of consecutive 0s at the start and the end of the string, and then print the maximum out of them.
Follow the steps below to solve the problem:
- Check if the frequency of ‘1’ in the string, S is equal to 0 or not. If found to be true, print the value of N as the result.
- Otherwise, perform the following steps:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findMaximumZeros(string str, int n)
{
int c0 = 0;
for ( int i = 0; i < n; ++i) {
if (str[i] == '0' )
c0++;
}
if (c0 == n) {
cout << n;
return ;
}
int mx = 0;
int cnt = 0;
for ( int i = 0; i < n; i++) {
if (str[i] == '0' )
cnt++;
else {
mx = max(mx, cnt);
cnt = 0;
}
}
mx = max(mx, cnt);
int start = 0, end = n - 1;
cnt = 0;
while (str[start] != '1' && start < n) {
cnt++;
start++;
}
while (str[end] != '1' && end >= 0) {
cnt++;
end--;
}
mx = max(mx, cnt);
cout << mx;
}
int main()
{
string s = "1001" ;
int n = s.size();
findMaximumZeros(s, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void findMaximumZeros(String str, int n)
{
int c0 = 0 ;
for ( int i = 0 ; i < n; ++i)
{
if (str.charAt(i) == '0' )
c0++;
}
if (c0 == n)
{
System.out.print(n);
return ;
}
int mx = 0 ;
int cnt = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (str.charAt(i) == '0' )
cnt++;
else
{
mx = Math.max(mx, cnt);
cnt = 0 ;
}
}
mx = Math.max(mx, cnt);
int start = 0 , end = n - 1 ;
cnt = 0 ;
while (str.charAt(start) != '1' && start < n)
{
cnt++;
start++;
}
while (str.charAt(end) != '1' && end >= 0 )
{
cnt++;
end--;
}
mx = Math.max(mx, cnt);
System.out.println(mx);
}
public static void main (String[] args)
{
String s = "1001" ;
int n = s.length();
findMaximumZeros(s, n);
}
}
|
Python3
def findMaximumZeros(string, n):
c0 = 0
for i in range (n):
if (string[i] = = '0' ):
c0 + = 1
if (c0 = = n):
print (n, end = "")
return
mx = 0
cnt = 0
for i in range (n):
if (string[i] = = '0' ):
cnt + = 1
else :
mx = max (mx, cnt)
cnt = 0
mx = max (mx, cnt)
start = 0
end = n - 1
cnt = 0
while (string[start] ! = '1' and start < n):
cnt + = 1
start + = 1
while (string[end] ! = '1' and end > = 0 ):
cnt + = 1
end - = 1
mx = max (mx, cnt)
print (mx, end = "")
if __name__ = = "__main__" :
s = "1001"
n = len (s)
findMaximumZeros(s, n)
|
C#
using System;
class GFG{
static void findMaximumZeros( string str, int n)
{
int c0 = 0;
for ( int i = 0; i < n; ++i)
{
if (str[i] == '0' )
c0++;
}
if (c0 == n)
{
Console.Write(n);
return ;
}
int mx = 0;
int cnt = 0;
for ( int i = 0; i < n; i++)
{
if (str[i] == '0' )
cnt++;
else
{
mx = Math.Max(mx, cnt);
cnt = 0;
}
}
mx = Math.Max(mx, cnt);
int start = 0, end = n - 1;
cnt = 0;
while (str[start] != '1' && start < n)
{
cnt++;
start++;
}
while (str[end] != '1' && end >= 0)
{
cnt++;
end--;
}
mx = Math.Max(mx, cnt);
Console.Write(mx);
}
static public void Main ()
{
string s = "1001" ;
int n = s.Length;
findMaximumZeros(s, n);
}
}
|
Javascript
<script>
function findMaximumZeros(str, n)
{
var c0 = 0;
for ( var i = 0; i < n; ++i) {
if (str[i] == '0' )
c0++;
}
if (c0 == n) {
document.write( n);
return ;
}
var mx = 0;
var cnt = 0;
for ( var i = 0; i < n; i++) {
if (str[i] == '0' )
cnt++;
else {
mx = Math.max(mx, cnt);
cnt = 0;
}
}
mx = Math.max(mx, cnt);
var start = 0, end = n - 1;
cnt = 0;
while (str[start] != '1' && start < n) {
cnt++;
start++;
}
while (str[end] != '1' && end >= 0) {
cnt++;
end--;
}
mx = Math.max(mx, cnt);
document.write( mx);
}
var s = "1001" ;
var n = s.length;
findMaximumZeros(s, n);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...