Minimum number of flips with rotation to make binary string alternating
Last Updated :
30 Nov, 2021
Given a binary string S of 0s and 1s. The task is to make the given string a sequence of alternate characters by using the below operations:
- Remove some prefix from the start and append it to the end.
- Flip some or every bit in the given string.
Print the minimum number of bits to be flipped to make the given string alternating.
Examples:
Input: S = "001"
Output: 0
Explanation:
No need to flip any element we can get alternating sequence by using left rotation: 010.
Input: S = "000001100"
Output: 3
Explanation:
Following steps to find minimum flips to get alternating string:
1. After rotating string 6 times towards left we will get: 100000001
2. Now we can apply flip operation as following: 101000001 -> 101010001 -> 101010101
Thus, minimum flips to make string alternating is 3.
Naive Approach: The naive approach is to take all N possible combinations and calculate the minimum number of bits To flip in each of those strings. Print the minimum count among all such combinations.
Time Complexity: O(N2), where N is the length of the string.
Auxiliary Space: O(N)
Efficient Approach: This can be solved by observing that the final string will be either of type "101010..." or "010101..." such that all 1s will either be at odd positions or at even positions. Follow the below steps to solve the problem:
- Create a prefix sum array where pref[i] means a number of changes required until index i.
- Create prefix arrays for both the above patterns.
- Check for every i, if substring[0, i] is appended at the end how many characters to be flipped required.
- Print the minimum number of flips among all the substrings in the above steps.
Why only odd length strings are considered for rotation and why rotation will have no effect on even length string?
I'll try to explain this with the below example,
Suppose you have the sequence 011000 which is of even length. Without rotation, you can change it to 010101 or 101010. Now suppose you choose to append 01 at the end. The sequence becomes 100001 which can be changed to 010101 or 101010. If you compare each character, you will see that this is the same case as that of without rotation. 1000 corresponds to either 0101 or 1010 in both cases and 01 to 01 or 10.
But now consider an odd length case, 01100. Without rotation, you can change it to 01010 or 10101. Now suppose you choose to append 01 at the end. The sequence becomes 10001 which can be changed to 01010 or 10101. Now if you compare each character, you will see that 100 corresponds to 010 or 101 in both cases but 01 corresponds to 01 when 100 is 010 in case of no rotation and 101 in case of rotation.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function that finds the minimum
// number of flips to make the
// binary string alternating if
// left circular rotation is allowed
int MinimumFlips(string s, int n)
{
int a[n];
for(int i = 0; i < n; i++)
{
a[i] = (s[i] == '1' ? 1 : 0);
}
// Initialize prefix arrays to store
// number of changes required to put
// 1s at either even or odd position
int oddone[n + 1];
int evenone[n + 1];
oddone[0] = 0;
evenone[0] = 0;
for(int i = 0; i < n; i++)
{
// If i is odd
if (i % 2 != 0)
{
// Update the oddone
// and evenone count
oddone[i + 1] = oddone[i] +
(a[i] == 1 ? 1 : 0);
evenone[i + 1] = evenone[i] +
(a[i] == 0 ? 1 : 0);
}
// Else i is even
else
{
// Update the oddone
// and evenone count
oddone[i + 1] = oddone[i] +
(a[i] == 0 ? 1 : 0);
evenone[i + 1] = evenone[i] +
(a[i] == 1 ? 1 : 0);
}
}
// Initialize minimum flips
int minimum = min(oddone[n], evenone[n]);
// Check if substring[0, i] is
// appended at end how many
// changes will be required
for(int i = 0; i < n; i++)
{
if (n % 2 != 0)
{
minimum = min(minimum,
oddone[n] -
oddone[i + 1] +
evenone[i + 1]);
minimum = min(minimum,
evenone[n] -
evenone[i + 1] +
oddone[i + 1]);
}
}
// Return minimum flips
return minimum;
}
// Driver Code
int main()
{
// Given String
string S = "000001100";
// Length of given string
int n = S.length();
// Function call
cout << (MinimumFlips(S, n));
}
// This code is contributed by chitranayal
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function that finds the minimum
// number of flips to make the
// binary string alternating if
// left circular rotation is allowed
static int MinimumFlips(String s, int n)
{
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = (s.charAt(i) == '1' ? 1 : 0);
}
// Initialize prefix arrays to store
// number of changes required to put
// 1s at either even or odd position
int[] oddone = new int[n + 1];
int[] evenone = new int[n + 1];
oddone[0] = 0;
evenone[0] = 0;
for (int i = 0; i < n; i++) {
// If i is odd
if (i % 2 != 0) {
// Update the oddone
// and evenone count
oddone[i + 1]
= oddone[i]
+ (a[i] == 1 ? 1 : 0);
evenone[i + 1]
= evenone[i]
+ (a[i] == 0 ? 1 : 0);
}
// Else i is even
else {
// Update the oddone
// and evenone count
oddone[i + 1]
= oddone[i]
+ (a[i] == 0 ? 1 : 0);
evenone[i + 1]
= evenone[i]
+ (a[i] == 1 ? 1 : 0);
}
}
// Initialize minimum flips
int minimum = Math.min(oddone[n],
evenone[n]);
// Check if substring[0, i] is
// appended at end how many
// changes will be required
for (int i = 0; i < n; i++) {
if (n % 2 != 0) {
minimum = Math.min(minimum,
oddone[n]
- oddone[i + 1]
+ evenone[i + 1]);
minimum = Math.min(minimum,
evenone[n]
- evenone[i + 1]
+ oddone[i + 1]);
}
}
// Return minimum flips
return minimum;
}
// Driver Code
public static void main(String[] args)
{
// Given String
String S = "000001100";
// Length of given string
int n = S.length();
// Function call
System.out.print(MinimumFlips(S, n));
}
}
Python3
# Python3 program for the above approach
# Function that finds the minimum
# number of flips to make the
# binary string alternating if
# left circular rotation is allowed
def MinimumFlips(s, n):
a = [0] * n
for i in range(n):
a[i] = 1 if s[i] == '1' else 0
# Initialize prefix arrays to store
# number of changes required to put
# 1s at either even or odd position
oddone = [0] * (n + 1)
evenone = [0] * (n + 1)
for i in range(n):
# If i is odd
if(i % 2 != 0):
# Update the oddone
# and evenone count
if(a[i] == 1):
oddone[i + 1] = oddone[i] + 1
else:
oddone[i + 1] = oddone[i] + 0
if(a[i] == 0):
evenone[i + 1] = evenone[i] + 1
else:
evenone[i + 1] = evenone[i] + 0
# Else i is even
else:
# Update the oddone
# and evenone count
if (a[i] == 0):
oddone[i + 1] = oddone[i] + 1
else:
oddone[i + 1] = oddone[i] + 0
if (a[i] == 1):
evenone[i + 1] = evenone[i] + 1
else:
evenone[i + 1] = evenone[i] + 0
# Initialize minimum flips
minimum = min(oddone[n], evenone[n])
# Check if substring[0, i] is
# appended at end how many
# changes will be required
for i in range(n):
if(n % 2 != 0):
minimum = min(minimum,
oddone[n] -
oddone[i + 1] +
evenone[i + 1])
minimum = min(minimum,
evenone[n] -
evenone[i + 1] +
oddone[i + 1])
# Return minimum flips
return minimum
# Driver Code
# Given String
S = "000001100"
# Length of given string
n = len(S)
# Function call
print(MinimumFlips(S, n))
# This code is contributed by Shivam Singh
C#
// C# program for the above approach
using System;
class GFG{
// Function that finds the minimum
// number of flips to make the
// binary string alternating if
// left circular rotation is allowed
static int MinimumFlips(String s, int n)
{
int[] a = new int[n];
for (int i = 0; i < n; i++)
{
a[i] = (s[i] == '1' ? 1 : 0);
}
// Initialize prefix arrays to store
// number of changes required to put
// 1s at either even or odd position
int[] oddone = new int[n + 1];
int[] evenone = new int[n + 1];
oddone[0] = 0;
evenone[0] = 0;
for (int i = 0; i < n; i++)
{
// If i is odd
if (i % 2 != 0)
{
// Update the oddone
// and evenone count
oddone[i + 1] = oddone[i] +
(a[i] == 1 ? 1 : 0);
evenone[i + 1] = evenone[i] +
(a[i] == 0 ? 1 : 0);
}
// Else i is even
else
{
// Update the oddone
// and evenone count
oddone[i + 1] = oddone[i] +
(a[i] == 0 ? 1 : 0);
evenone[i + 1] = evenone[i] +
(a[i] == 1 ? 1 : 0);
}
}
// Initialize minimum flips
int minimum = Math.Min(oddone[n],
evenone[n]);
// Check if substring[0, i] is
// appended at end how many
// changes will be required
for (int i = 0; i < n; i++)
{
if (n % 2 != 0)
{
minimum = Math.Min(minimum, oddone[n] -
oddone[i + 1] +
evenone[i + 1]);
minimum = Math.Min(minimum, evenone[n] -
evenone[i + 1] +
oddone[i + 1]);
}
}
// Return minimum flips
return minimum;
}
// Driver Code
public static void Main(String[] args)
{
// Given String
String S = "000001100";
// Length of given string
int n = S.Length;
// Function call
Console.Write(MinimumFlips(S, n));
}
}
// This code is contributed by Rajput-Ji
JavaScript
<script>
// JavaScript program for the
// above approach
// Function that finds the minimum
// number of flips to make the
// binary string alternating if
// left circular rotation is allowed
function MinimumFlips(s, n)
{
let a = Array.from({length: n+1}, (_, i) => 0);
for (let i = 0; i < n; i++) {
a[i] = (s[i] == '1' ? 1 : 0);
}
// Initialize prefix arrays to store
// number of changes required to put
// 1s at either even or odd position
let oddone = Array.from({length: n+1}, (_, i) => 0);
let evenone = Array.from({length: n+1}, (_, i) => 0);
oddone[0] = 0;
evenone[0] = 0;
for (let i = 0; i < n; i++) {
// If i is odd
if (i % 2 != 0) {
// Update the oddone
// and evenone count
oddone[i + 1]
= oddone[i]
+ (a[i] == 1 ? 1 : 0);
evenone[i + 1]
= evenone[i]
+ (a[i] == 0 ? 1 : 0);
}
// Else i is even
else {
// Update the oddone
// and evenone count
oddone[i + 1]
= oddone[i]
+ (a[i] == 0 ? 1 : 0);
evenone[i + 1]
= evenone[i]
+ (a[i] == 1 ? 1 : 0);
}
}
// Initialize minimum flips
let minimum = Math.min(oddone[n],
evenone[n]);
// Check if substring[0, i] is
// appended at end how many
// changes will be required
for (let i = 0; i < n; i++) {
if (n % 2 != 0) {
minimum = Math.min(minimum,
oddone[n]
- oddone[i + 1]
+ evenone[i + 1]);
minimum = Math.min(minimum,
evenone[n]
- evenone[i + 1]
+ oddone[i + 1]);
}
}
// Return minimum flips
return minimum;
}
// Driver Code
// Given String
let S = "000001100";
// Length of given string
let n = S.length;
// Function call
document.write(MinimumFlips(S, n));
</script>
Time Complexity: O(N), where N is the length of the given string.
Auxiliary Space: O(N)
Similar Reads
Number of flips to make binary string alternate | Set 1
Given a binary string, that is it contains only 0s and 1s. We need to make this string a sequence of alternate characters by flipping some of the bits, our goal is to minimize the number of bits to be flipped. Examples : Input : str = â001â Output : 1 Minimum number of flips required = 1 We can flip
8 min read
Minimum number of replacements to make the binary string alternating | Set 2
Given a binary string str, the task is to find the minimum number of characters in the string that have to be replaced in order to make the string alternating (i.e. of the form 01010101... or 10101010...).Examples: Input: str = "1100" Output: 2 Replace 2nd character with '0' and 3rd character with '
5 min read
Minimum number of characters to be removed to make a binary string alternate
Given a binary string, the task is to find minimum number of characters to be removed from it so that it becomes alternate. A binary string is alternate if there are no two consecutive 0s or 1s.Examples : Input : s = "000111" Output : 4 We need to delete two 0s and two 1s to make string alternate. I
4 min read
Minimum number of flips to make a Binary String increasing
Given a binary string S, the task is to find the minimum number of characters the needed to be flipped to make the given binary string increasing. Example: Input: S = "00110"Output: 1Explanation: Flip S[4] = '0' to '1' of the string modifies the given string to "00111". Therefore, the minimum number
6 min read
Minimum substring reversals required to make given Binary String alternating
Given a binary string S of length N, the task is to count the minimum number substrings of S that is required to be reversed to make the string S alternating. If it is not possible to make string alternating, then print "-1". Examples: Input: S = "10001110"Output: 2Explanation:In the first operation
7 min read
Number of alternating substrings from a given Binary String
Given a binary string of size N, the task is to count the number of alternating substrings that are present in the string S. Examples: Input: S = "0010"Output: 7Explanation: All the substring of the string S are: {"0", "00", "001", "0010", "0", "01", "010", "1", "10", "0"}Strings that are alternatin
13 min read
Minimum swaps required to make a binary string alternating
You are given a binary string of even length (2N) and an equal number of 0's (N) and 1's (N). What is the minimum number of swaps to make the string alternating? A binary string is alternating if no two consecutive elements are equal. Examples: Input : 000111Output : 1Explanation : Swap index 2 and
11 min read
Minimum number of swaps to make two binary string equal
Given two binary strings of equal length, the task is to find the minimum number of swaps to make them equal. Swapping between two characters from two different strings is only allowed, return -1 if strings can't be made equal. Examples: Input: s1 = "0011", s2 = "1111" Output: 1Explanation:Swap s1[0
12 min read
Minimum move to end operations to make all strings equal
Given n strings that are permutations of each other. We need to make all strings same with an operation that takes front character of any string and moves it to the end. Examples: Input: n = 2, arr[] = {"molzv", "lzvmo"}Output: 2Explanation: In first string, we remove first element("m") from first s
13 min read
Minimum number of flipping adjacent bits required to make given Binary Strings equal
Given two binary strings s1[] and s2[] of the same length N, the task is to find the minimum number of operations to make them equal. Print -1 if it is impossible to do so. One operation is defined as choosing two adjacent indices of one of the binary string and inverting the characters at those pos
7 min read