Minimum number of operations required to maximize the Binary String
Last Updated :
20 Apr, 2022
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 from the string.
Input: S = "110001001"
Output: 2
Explanation: Swapping S[3] with S[6] and S[4] with S[9], we get 111100000 which is the required string
Naive Approach:
It can be observed that, in order to maximize the required string, the characters should be swapped such that all 0s should be on the right and all the 1s are on the left. Therefore, the string needs to be modified to a "1...10...0" sequence.
Follow the below steps to solve the problem:
- Count the number of 1s in the string S, say cnt1.
- Count the number of 0s in substring S[0], ..., S[cnt1-1], say cnt0.
- Print cnt0 as the required answer.
Below is the implementation of above approach:
C++
// C++ Program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the number
// of operations required
int minOperation(string s, int n)
{
// Count of 1's
int cnt1 = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '1')
cnt1++;
}
// Count of 0's upto (cnt1)-th index
int cnt0 = 0;
for (int i = 0; i < cnt1; i++) {
if (s[i] == '0')
cnt0++;
}
// Return the answer
return cnt0;
}
// Driver Code
int main()
{
int n = 8;
string s = "01001011";
int ans = minOperation(s, n);
cout << ans << endl;
}
Java
// Java program to implement
// the above approach
class GFG{
// Function to find the number
// of operations required
static int minOperation(String s, int n)
{
// Count of 1's
int cnt1 = 0;
for(int i = 0; i < n; i++)
{
if (s.charAt(i) == '1')
cnt1++;
}
// Count of 0's upto (cnt1)-th index
int cnt0 = 0;
for(int i = 0; i < cnt1; i++)
{
if (s.charAt(i) == '0')
cnt0++;
}
// Return the answer
return cnt0;
}
// Driver Code
public static void main(String[] args)
{
int n = 8;
String s = "01001011";
int ans = minOperation(s, n);
System.out.print(ans + "\n");
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 program to implement
# the above approach
# Function to find the number
# of operations required
def minOperation(s, n):
# Count of 1's
cnt1 = 0;
for i in range(n):
if (ord(s[i]) == ord('1')):
cnt1 += 1;
# Count of 0's upto (cnt1)-th index
cnt0 = 0;
for i in range(0, cnt1):
if (s[i] == '0'):
cnt0 += 1;
# Return the answer
return cnt0;
# Driver Code
if __name__ == '__main__':
n = 8;
s = "01001011";
ans = minOperation(s, n);
print(ans);
# This code is contributed by Amit Katiyar
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to find the number
// of operations required
static int minOperation(String s, int n)
{
// Count of 1's
int cnt1 = 0;
for(int i = 0; i < n; i++)
{
if (s[i] == '1')
cnt1++;
}
// Count of 0's upto (cnt1)-th index
int cnt0 = 0;
for(int i = 0; i < cnt1; i++)
{
if (s[i] == '0')
cnt0++;
}
// Return the answer
return cnt0;
}
// Driver Code
public static void Main(String[] args)
{
int n = 8;
String s = "01001011";
int ans = minOperation(s, n);
Console.Write(ans + "\n");
}
}
// This code is contributed by PrinciRaj1992
JavaScript
<script>
// JavaScript Program to implement
// the above approach
// Function to find the number
// of operations required
function minOperation(s,n)
{
// Count of 1's
let cnt1 = 0;
for (let i = 0; i < n; i++) {
if (s[i] == '1')
cnt1++;
}
// Count of 0's upto (cnt1)-th index
let cnt0 = 0;
for (let i = 0; i < cnt1; i++) {
if (s[i] == '0')
cnt0++;
}
// Return the answer
return cnt0;
}
// Driver Code
let n = 8;
let s = "01001011";
let ans = minOperation(s, n);
document.write(ans,"</br>");
// This code is contributed by shinjanpatra
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Efficient Approach:
The above approach can be further optimized using Two Pointers technique. Follow the below steps to solve the problem:
- Set two pointers i = 0 and j = S.length() - 1 and iterate until i exceeds j.
- Increase count, if S[i] is equal to '0' and S[j] is equal to '1'.
- Increment i if S[i] is '1' until a '0' appears. Similarly, decrease j until S[j] is equal to '1'.
- Print count as the required answer.
Below is the implementation of above approach:
C++
// C++ Program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the number
// of operations required
int minOperation(string s, int n)
{
int ans = 0;
int i = 0, j = n - 1;
while (i < j) {
// Swap 0's and 1's
if (s[i] == '0' && s[j] == '1') {
ans++;
i++;
j--;
continue;
}
if (s[i] == '1') {
i++;
}
if (s[j] == '0') {
j--;
}
}
// Return the answer
return ans;
}
// Driver Code
int main()
{
int n = 8;
string s = "10100101";
int ans = minOperation(s, n);
cout << ans << endl;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to find the number
// of operations required
static int minOperation(String s, int n)
{
int ans = 0;
int i = 0, j = n - 1;
while (i < j)
{
// Swap 0's and 1's
if (s.charAt(i) == '0' &&
s.charAt(j) == '1')
{
ans++;
i++;
j--;
continue;
}
if (s.charAt(i) == '1')
{
i++;
}
if (s.charAt(j) == '0')
{
j--;
}
}
// Return the answer
return ans;
}
// Driver code
public static void main (String[] args)
{
int n = 8;
String s = "10100101";
System.out.println(minOperation(s, n));
}
}
// This code is contributed by offbeat
Python3
# Python3 program to implement
# the above approach
# Function to find the number
# of operations required
def minOperation(s, n):
ans = 0;
i = 0; j = n - 1;
while (i < j):
# Swap 0's and 1's
if (s[i] == '0' and s[j] == '1'):
ans += 1;
i += 1;
j -= 1;
continue;
if (s[i] == '1'):
i += 1;
if (s[j] == '0'):
j -= 1;
# Return the answer
return ans;
# Driver code
if __name__ == '__main__':
n = 8;
s = "10100101";
print(minOperation(s, n));
# This code is contributed by sapnasingh4991
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to find the number
// of operations required
static int minOperation(String s, int n)
{
int ans = 0;
int i = 0, j = n - 1;
while (i < j)
{
// Swap 0's and 1's
if (s[i] == '0' &&
s[j] == '1')
{
ans++;
i++;
j--;
continue;
}
if (s[i] == '1')
{
i++;
}
if (s[j] == '0')
{
j--;
}
}
// Return the answer
return ans;
}
// Driver code
public static void Main(String[] args)
{
int n = 8;
String s = "10100101";
Console.WriteLine(minOperation(s, n));
}
}
// This code is contributed by sapnasingh4991
JavaScript
<script>
// JavaScript program to implement
// the above approach
// Function to find the number
// of operations required
function minOperation(s, n)
{
let ans = 0;
let i = 0, j = n - 1;
while (i < j)
{
// Swap 0's and 1's
if (s[i] == '0' &&
s[j] == '1')
{
ans++;
i++;
j--;
continue;
}
if (s[i]== '1')
{
i++;
}
if (s[j] == '0')
{
j--;
}
}
// Return the answer
return ans;
}
// Driver Code
let n = 8;
let s = "10100101";
document.write(minOperation(s, n));
</script>
Output:
2
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Minimum number of operations required to obtain a given Binary String 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 =
8 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 Deci-Binary numbers required to obtain a given sum S Given a numeric string S representing a positive decimal integer, the task is to find the minimum number of positive Deci-Binary numbers required to obtain the sum S. Deci-Binary Numbers: Decimal numbers consisting of only 0s and 1s as its digits. Examples: Input: S = "31"Output: 3Explanation: S can
5 min read
Minimum operations required to convert a binary string to all 0s or all 1s Given a binary string str, the task is to find the minimum number of operations required to make all the characters of the string same i.e. either the resultant string contains all 0s or all 1s. In a single operation, any block of consecutive 0s can be converted to a block of consecutive 1s of the s
4 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