Minimum number of operations required to obtain a given Binary String
Last Updated :
04 Aug, 2021
Given a binary strings S of length N, the task is to obtain S from a string, say T, of length N consisting only of zeroes, by minimum number of operations. Each operation involves choosing any index i from string S and flipping all the bits at indices [i, N - 1] of the string T.
Examples:
Input: S = "101"
Output: 3
Explanation:
"000" -> "111" -> "100" -> "101".
Therefore, the minimum number of steps required is 3.
Input: S = "10111"
Output: 3
Explanation:
"00000" -> "11111" -> "10000" -> "10111".
Therefore, the minimum number of steps required is 3.
Approach:
The idea is to find the first occurrence of 1 in the given string S and perform the given operation at that index. After this step, for every mismatch in the character of S and T at a particular index, repeat the operation.
Follow the steps below:
- Iterate over S and mark the first occurrence of 1.
- Initialize two variables, say last and ans, where last stores the character (0 or 1) for which the last operation was performed and ans keeps count of the minimum number of steps required.
- Iterate from the first occurrence of 1 till the end of the string.
- If the current character is 0 and last = 1, then increment the count of ans, and set last = 0.
- Otherwise, if last = 0 and the current character is 1, then set last = 1.
- Return the final value of ans.
Below is the implementation of the above approach:
C++
// C++ Program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the minimum
// number of operations required
// to obtain the string s
int minOperations(string s)
{
int n = s.size();
int pos = -1;
// Iterate the string s
for (int i = 0; i < s.size(); i++) {
// If first occurrence of 1
// is found
if (s[i] == '1') {
// Mark the index
pos = i;
break;
}
}
// Base case: If no 1 occurred
if (pos == -1) {
// No operations required
return 0;
}
// Stores the character
// for which last operation
// was performed
int last = 1;
// Stores minimum number
// of operations
int ans = 1;
// Iterate from pos to n
for (int i = pos + 1; i < n; i++) {
// Check if s[i] is 0
if (s[i] == '0') {
// Check if last operation was
// performed because of 1
if (last == 1) {
ans++;
// Set last to 0
last = 0;
}
}
else {
if (last == 0) {
// Check if last operation was
// performed because of 0
ans++;
// Set last to 1
last = 1;
}
}
}
// Return the answer
return ans;
}
// Driver Code
int main()
{
string s = "10111";
cout << minOperations(s);
return 0;
}
Java
// Java program to implement the
// above approach
import java.util.*;
class GFG{
// Function to find the minimum
// number of operations required
// to obtain the string s
static int minOperations(String s)
{
int n = s.length();
int pos = -1;
// Iterate the string s
for(int i = 0; i < s.length(); i++)
{
// If first occurrence of 1
// is found
if (s.charAt(i) == '1')
{
// Mark the index
pos = i;
break;
}
}
// Base case: If no 1 occurred
if (pos == -1)
{
// No operations required
return 0;
}
// Stores the character
// for which last operation
// was performed
int last = 1;
// Stores minimum number
// of operations
int ans = 1;
// Iterate from pos to n
for(int i = pos + 1; i < n; i++)
{
// Check if s[i] is 0
if (s.charAt(i) == '0')
{
// Check if last operation was
// performed because of 1
if (last == 1)
{
ans++;
// Set last to 0
last = 0;
}
}
else
{
if (last == 0)
{
// Check if last operation was
// performed because of 0
ans++;
// Set last to 1
last = 1;
}
}
}
// Return the answer
return ans;
}
// Driver code
public static void main(String[] args)
{
String s = "10111";
System.out.println(minOperations(s));
}
}
// This code is contributed by offbeat
Python3
# Python3 program to implement
# the above approach
# Function to find the minimum
# number of operations required
# to obtain the string s
def minOperations(s):
n = len(s)
pos = -1
# Iterate the string s
for i in range(len(s)):
# If first occurrence of 1
# is found
if (s[i] == '1'):
# Mark the index
pos = i
break
# Base case: If no 1 occurred
if (pos == -1):
# No operations required
return 0
# Stores the character
# for which last operation
# was performed
last = 1
# Stores minimum number
# of operations
ans = 1
# Iterate from pos to n
for i in range(pos + 1, n):
# Check if s[i] is 0
if (s[i] == '0'):
# Check if last operation was
# performed because of 1
if (last == 1):
ans += 1
# Set last to 0
last = 0
else:
if (last == 0):
# Check if last operation was
# performed because of 0
ans += 1
# Set last to 1
last = 1
# Return the answer
return ans
# Driver Code
if __name__ == "__main__":
s = "10111"
print(minOperations(s))
# This code is contributed by chitranayal
C#
// C# program to implement the
// above approach
using System;
class GFG{
// Function to find the minimum
// number of operations required
// to obtain the string s
static int minOperations(String s)
{
int n = s.Length;
int pos = -1;
// Iterate the string s
for(int i = 0; i < s.Length; i++)
{
// If first occurrence of 1
// is found
if (s[i] == '1')
{
// Mark the index
pos = i;
break;
}
}
// Base case: If no 1 occurred
if (pos == -1)
{
// No operations required
return 0;
}
// Stores the character
// for which last operation
// was performed
int last = 1;
// Stores minimum number
// of operations
int ans = 1;
// Iterate from pos to n
for(int i = pos + 1; i < n; i++)
{
// Check if s[i] is 0
if (s[i] == '0')
{
// Check if last operation was
// performed because of 1
if (last == 1)
{
ans++;
// Set last to 0
last = 0;
}
}
else
{
if (last == 0)
{
// Check if last operation was
// performed because of 0
ans++;
// Set last to 1
last = 1;
}
}
}
// Return the answer
return ans;
}
// Driver code
public static void Main(string[] args)
{
String s = "10111";
Console.Write(minOperations(s));
}
}
// This code is contributed by rock_cool
JavaScript
<script>
// Javascript Program to implement
// the above approach
// Function to find the minimum
// number of operations required
// to obtain the string s
function minOperations(s)
{
let n = s.length;
let pos = -1;
// Iterate the string s
for (let i = 0; i < s.length; i++) {
// If first occurrence of 1
// is found
if (s[i] == '1') {
// Mark the index
pos = i;
break;
}
}
// Base case: If no 1 occurred
if (pos == -1) {
// No operations required
return 0;
}
// Stores the character
// for which last operation
// was performed
let last = 1;
// Stores minimum number
// of operations
let ans = 1;
// Iterate from pos to n
for (let i = pos + 1; i < n; i++) {
// Check if s[i] is 0
if (s[i] == '0') {
// Check if last operation was
// performed because of 1
if (last == 1) {
ans++;
// Set last to 0
last = 0;
}
}
else {
if (last == 0) {
// Check if last operation was
// performed because of 0
ans++;
// Set last to 1
last = 1;
}
}
}
// Return the answer
return ans;
}
let s = "10111";
document.write(minOperations(s));
// This code is contributed by suresh07.
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Minimum number of operations required to maximize the Binary String Given a binary string S, the task is to find the minimum number of swaps required to be performed to maximize the value represented by S. Examples: Input: S = "1010001" Output: 1 Explanation: Swapping S[2] and S[7], modifies the string to 1110000, thus, maximizing the number that can be generated fr
7 min read
Minimum number of operations required to sum to binary string S Given a binary string, S. Find the minimum number of operations required to be performed on the number zero to convert it to the number represented by S.It is allowed to perform operations of 2 types: Add 2xSubtract 2xNote: Start performing operations on 0. Examples: Input : 100 Output : 1 Explanati
15+ min read
Minimum operations require to balance Binary String Given a binary string S of length N, the task is to find the minimum number of operations required to balance (equal number of '0' and '1') the given binary string, you can perform two types of operations one at a time, which is given below. Remove a character from the front of the given string.Remo
7 min read
Minimum given operations required to convert a given binary string to all 1's Given a binary number as a string str of length L. The task is to find the minimum number of operations needed so that the number becomes 2L-1, that is a string consisting of only 1's of the length L. In each operation, the number N can be replaced by N xor (N + 1). Examples: Input: str = "10010111"
7 min read
Minimum number of Binary strings to represent a Number Given a number N. The task is to find the minimum number of binary strings required to represent the given number as the sum of the binary strings.Examples: Input : 131 Output : Minimum Number of binary strings needed: 3 111 10 10 Input : 564 Output :Minimum Number of binary strings needed: 6 111 11
7 min read