Python3 Program to Find Maximum number of 0s placed consecutively at the start and end in any rotation of a Binary String
Last Updated :
05 Sep, 2024
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.
Below is the implementation of the above approach:
Python
# Python3 program for the above approach
# Function to find the maximum sum of
# consecutive 0s present at the start
# and end of a string present in any
# of the rotations of the given string
def findMaximumZeros(st, n):
# Check if all the characters
# in the string are 0
c0 = 0
# Iterate over characters
# of the string
for i in range(n):
if (st[i] == '0'):
c0 += 1
# If the frequency of '1' is 0
if (c0 == n):
# Print n as the result
print(n)
return
# Concatenate the string
# with itself
s = st + st
# Stores the required result
mx = 0
# Generate all rotations of the string
for i in range(n):
# Store the number of consecutive 0s
# at the start and end of the string
cs = 0
ce = 0
# Count 0s present at the start
for j in range(i, i + n):
if (s[j] == '0'):
cs += 1
else:
break
# Count 0s present at the end
for j in range(i + n - 1, i - 1, -1):
if (s[j] == '0'):
ce += 1
else:
break
# Calculate the sum
val = cs + ce
# Update the overall
# maximum sum
mx = max(val, mx)
# Print the result
print(mx)
# Driver Code
if __name__ == "__main__":
# Given string
s = "1001"
# Store the size of the string
n = len(s)
findMaximumZeros(s, n)
# This code is contributed by ukasp.
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:
Below is the implementation of the above approach:
Python
# Python3 program for the above approach
# Function to find the maximum sum of
# consecutive 0s present at the start
# and end of any rotation of the string str
def findMaximumZeros(string, n):
# Stores the count of 0s
c0 = 0
for i in range(n):
if (string[i] == '0'):
c0 += 1
# If the frequency of '1' is 0
if (c0 == n):
# Print n as the result
print(n, end = "")
return
# Stores the required sum
mx = 0
# Find the maximum consecutive
# length of 0s present in the string
cnt = 0
for i in range(n):
if (string[i] == '0'):
cnt += 1
else:
mx = max(mx, cnt)
cnt = 0
# Update the overall maximum sum
mx = max(mx, cnt)
# Find the number of 0s present at
# the start and end of the string
start = 0
end = n - 1
cnt = 0
# Update the count of 0s at the start
while (string[start] != '1' and start < n):
cnt += 1
start += 1
# Update the count of 0s at the end
while (string[end] != '1' and end >= 0):
cnt += 1
end -= 1
# Update the maximum sum
mx = max(mx, cnt)
# Print the result
print(mx, end = "")
# Driver Code
if __name__ == "__main__":
# Given string
s = "1001"
# Store the size of the string
n = len(s)
findMaximumZeros(s, n)
# This code is contributed by AnkThon
Time Complexity: O(N)
Auxiliary Space: O(1)
Please refer complete article on
Maximum number of 0s placed consecutively at the start and end in any rotation of a Binary String for more details!
Similar Reads
Javascript Program to Find Maximum number of 0s placed consecutively at the start and end in any rotation of a Binary String 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: 2Explanation:All possible rotations of the string are:"1001": Count of 0s at the start = 0;
5 min read
Maximum number of 0s placed consecutively at the start and end in any rotation of a Binary String 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: 2Explanation:All possible rotations of the string are:"1001": Count of 0s at the start = 0;
15+ min read
Maximize partitions in given Binary String having same ratio of 0s and 1s Given a binary string S of size N, the task is to find the maximum number of partitions of string S such that the ratio of the number of 0 and 1 in all the partitions are the same. Examples: Input: S = "010100001"Output: 3Explanation: The substring [0, 2], [3, 5] and [6, 8] have the same ratio of '0
9 min read
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
Replace '?' to convert given string to a binary string with maximum count of '0' and "10" Given string str, consisting of three different types of characters '0', '1' and '?', the task is to convert the given string to a binary string by replacing the '?' characters with either '0' or '1' such that the count of 0s and 10 in the binary string is maximum. Examples: Input: str = 10?0?11Outp
4 min read
Minimum count of 0s to be removed from given Binary string to make all 1s occurs consecutively Given a binary string S of size N, the task is to find the minimum numbers of 0s that must be removed from the string S such that all the 1s occurs consecutively. Examples: Input: S = "010001011" Output: 4 Explanation: Removing the characters { S[2], S[3], S[4], S[6] } from the string S modifies the
7 min read