Minimum number of flipping adjacent bits required to make given Binary Strings equal
Last Updated :
08 May, 2023
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 positions, i.e, 1 to 0 and vice-versa.
Examples:
Input: s1[] = "0101", s2[] = "1111"
Output: 2
Explanation: Invert the characters at 1 and 2 indices in the first string and at 0 and 1 indices in the second string to make them equal as 0011. There are other ways to do also like converting to 1111, etc.
Input: s1[] = "011", s2[] = "111"
Output: -1
Approach: The idea is to linearly traverse both strings and if at any index the characters are different than invert the ith and (i+1)th character in the string s1[]. Follow the steps below to solve the problem:
- Initialize the variable count as 0 to store the answer.
- Iterate over the range [0, N] using the variable i and perform the following steps:
- If s1[i] is not equal to s2[i], then do the following tasks:
- If s1[i] is equal to 1, then change it to 0, else, change it to 1.
- Similarly, if s1[i+1] is equal to 1, then change it to 0, else, change it to 1.
- Finally, increase the value of count by 1.
- If s1[] is equal to s2[], then return the value of count as the answer else return -1.
Below is the implementation of the above approach.
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the minimum number
// of inversions required.
int find_Min_Inversion(int n, string s1, string s2)
{
// Initializing the answer
int count = 0;
// Iterate over the range
for (int i = 0; i < n - 1; i++) {
if (s1[i] != s2[i]) {
// If s1[i]!=s2[i], then inverse
// the characters at i snd (i+1)
// positions in s1.
if (s1[i] == '1') {
s1[i] = '0';
}
else {
s1[i] = '1';
}
if (s1[i + 1] == '1') {
s1[i + 1] = '0';
}
else {
s1[i + 1] = '1';
}
// Adding 1 to counter
// if characters are not same
count++;
}
}
if (s1 == s2) {
return count;
}
return -1;
}
// Driver Code
int main()
{
int n = 4;
string s1 = "0101";
string s2 = "1111";
cout << find_Min_Inversion(n, s1, s2) << endl;
return 0;
}
C
// C program for the above approach
#include <stdio.h>
#include <string.h>
// Function to find the minimum number
// of inversions required.
int find_Min_Inversion(int n, char s1[1000], char s2[1000])
{
// Initializing the answer
int count = 0;
// Iterate over the range
for (int i = 0; i < n - 1; i++) {
if (s1[i] != s2[i]) {
// If s1[i]!=s2[i], then inverse
// the characters at i snd (i+1)
// positions in s1.
if (s1[i] == '1') {
s1[i] = '0';
}
else {
s1[i] = '1';
}
if (s1[i + 1] == '1') {
s1[i + 1] = '0';
}
else {
s1[i + 1] = '1';
}
// Adding 1 to counter
// if characters are not same
count++;
}
}
if (strcmp(s1, s2) != -1) {
return count;
}
return -1;
}
// Driver Code
int main()
{
int n = 4;
char s1[1000] = "0101";
char s2[1000] = "1111";
printf("%d\n", find_Min_Inversion(n, s1, s2));
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function to find the minimum number
// of inversions required.
static int find_Min_Inversion(int n, char[] s1,
char[] s2)
{
// Initializing the answer
int count = 0;
// Iterate over the range
for (int i = 0; i < n - 1; i++) {
if (s1[i] != s2[i]) {
// If s1[i]!=s2[i], then inverse
// the characters at i snd (i+1)
// positions in s1.
if (s1[i] == '1') {
s1[i] = '0';
}
else {
s1[i] = '1';
}
if (s1[i + 1] == '1') {
s1[i + 1] = '0';
}
else {
s1[i + 1] = '1';
}
// Adding 1 to counter
// if characters are not same
count++;
}
}
if (String.copyValueOf(s1).equals(
String.copyValueOf(s2))) {
return count;
}
return -1;
}
// Driver Code
public static void main(String[] args)
{
int n = 4;
String s1 = "0101";
String s2 = "1111";
System.out.print(
find_Min_Inversion(n, s1.toCharArray(),
s2.toCharArray())
+ "\n");
}
}
// This code is contributed by umadevi9616
Python3
# Python 3 program for the above approach
# Function to find the minimum number
# of inversions required.
def find_Min_Inversion(n, s1, s2):
# Initializing the answer
count = 0
# Iterate over the range
s1 = list(s1)
s2 = list(s2)
for i in range(n - 1):
if (s1[i] != s2[i]):
# If s1[i]!=s2[i], then inverse
# the characters at i snd (i+1)
# positions in s1.
if (s1[i] == '1'):
s1[i] = '0'
else:
s1[i] = '1'
if (s1[i + 1] == '1'):
s1[i + 1] = '0'
else:
s1[i + 1] = '1'
# Adding 1 to counter
# if characters are not same
count += 1
s1 = ''.join(s1)
s2 = ''.join(s2)
if (s1 == s2):
return count
return -1
# Driver Code
if __name__ == '__main__':
n = 4
s1 = "0101"
s2 = "1111"
print(find_Min_Inversion(n, s1, s2))
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# program for the above approach
using System;
class GFG {
// Function to find the minimum number
// of inversions required.
static int find_Min_Inversion(int n, char[] s1,
char[] s2)
{
// Initializing the answer
int count = 0;
// Iterate over the range
for (int i = 0; i < n - 1; i++) {
if (s1[i] != s2[i]) {
// If s1[i]!=s2[i], then inverse
// the characters at i snd (i+1)
// positions in s1.
if (s1[i] == '1') {
s1[i] = '0';
}
else {
s1[i] = '1';
}
if (s1[i + 1] == '1') {
s1[i + 1] = '0';
}
else {
s1[i + 1] = '1';
}
// Adding 1 to counter
// if characters are not same
count++;
}
}
if (new string(s1) == new string(s2)) {
return count;
}
return -1;
}
// Driver Code
public static void Main(string[] args)
{
int n = 4;
string s1 = "0101";
string s2 = "1111";
// Function call
Console.Write(find_Min_Inversion(n,
s1.ToCharArray(),
s2.ToCharArray())
+ "\n");
}
}
// This code is contributed by phasing17
JavaScript
<script>
// Java program for the above approach
// Function to find the minimum number
// of inversions required.
function find_Min_Inversion(n, s1, s2)
{
// Initializing the answer
let count = 0;
// Iterate over the range
for (let i = 0; i < n - 1; i++) {
if (s1[i] != s2[i]) {
// If s1[i]!=s2[i], then inverse
// the characters at i snd (i+1)
// positions in s1.
if (s1[i] = '1') {
s1[i] = '0';
}
else {
s1[i] = '1';
}
if (s1[i + 1] = '1') {
s1[i + 1] = '0';
}
else {
s1[i + 1] = '1';
}
// Adding 1 to counter
// if characters are not same
count++;
}
}
if (s1 != s2) {
return count;
}
return -1;
}
// Driver Code
let n = 4;
let s1 = "0101";
let s2 = "1111";
document.write(find_Min_Inversion(n, s1, s2));
// This code is contributed by shivanisinghss2110
</script>
Time Complexity: O(N), The time complexity is O(n), where n is the length of the strings s1 and s2. This is because the program iterates over the range of size n-1 once and performs constant time operations (comparisons and assignments) within each iteration.
Auxiliary Space: O(1), The space complexity is O(1), since the program only uses a constant amount of extra space regardless of the input size.
Similar Reads
Minimum number of flips or swaps of adjacent characters required to make two strings equal Given two binary strings A and B of length N, the task is to count the minimum number of operations required to make the two given strings equal by either swapping adjacent characters or flipping any character of the string A. Examples: Input: A = "100", B = "001"Output: 2Explanation: Flipping chara
12 min read
Minimize flipping of bits in given Binary string to make count of 10 equal to 01 Given binary string str, the task is to choose any index and change into 0 or 1, and do this in minimum steps such that the count of substring 01 is equal to 10. Examples: Input: str = "01101"Output: 01100Explanation: 01 as a substring repeat 2 times in a string, 10 as a substring repeat 1 times in
5 min read
Minimum Count of Bit flips required to make a Binary String Palindromic Given an integer N, the task is to find the minimum number of bits required to be flipped to convert the binary representation of N into a palindrome. Examples: Input: N = 12 Output: 2 Explanation: Binary String representing 12 = "1100". To make "1100" a palindrome, convert the string to "0110". The
7 min read
Minimum flips or swapping of adjacent characters required to make a string equal to another Given two binary strings A and B of length N, the task is to convert the string A to B by either flipping any character of A or swapping adjacent characters of A minimum number of times. If it is not possible to make both the strings equal, print -1. Examples: Input: A = "10010010", B = "00001000" O
6 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