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++
#include <bits/stdc++.h>
using namespace std;
int minOperation(string s, int n)
{
int cnt1 = 0;
for ( int i = 0; i < n; i++) {
if (s[i] == '1' )
cnt1++;
}
int cnt0 = 0;
for ( int i = 0; i < cnt1; i++) {
if (s[i] == '0' )
cnt0++;
}
return cnt0;
}
int main()
{
int n = 8;
string s = "01001011" ;
int ans = minOperation(s, n);
cout << ans << endl;
}
|
Java
class GFG{
static int minOperation(String s, int n)
{
int cnt1 = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (s.charAt(i) == '1' )
cnt1++;
}
int cnt0 = 0 ;
for ( int i = 0 ; i < cnt1; i++)
{
if (s.charAt(i) == '0' )
cnt0++;
}
return cnt0;
}
public static void main(String[] args)
{
int n = 8 ;
String s = "01001011" ;
int ans = minOperation(s, n);
System.out.print(ans + "\n" );
}
}
|
Python3
def minOperation(s, n):
cnt1 = 0 ;
for i in range (n):
if ( ord (s[i]) = = ord ( '1' )):
cnt1 + = 1 ;
cnt0 = 0 ;
for i in range ( 0 , cnt1):
if (s[i] = = '0' ):
cnt0 + = 1 ;
return cnt0;
if __name__ = = '__main__' :
n = 8 ;
s = "01001011" ;
ans = minOperation(s, n);
print (ans);
|
C#
using System;
class GFG{
static int minOperation(String s, int n)
{
int cnt1 = 0;
for ( int i = 0; i < n; i++)
{
if (s[i] == '1' )
cnt1++;
}
int cnt0 = 0;
for ( int i = 0; i < cnt1; i++)
{
if (s[i] == '0' )
cnt0++;
}
return cnt0;
}
public static void Main(String[] args)
{
int n = 8;
String s = "01001011" ;
int ans = minOperation(s, n);
Console.Write(ans + "\n" );
}
}
|
Javascript
<script>
function minOperation(s,n)
{
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 cnt0;
}
let n = 8;
let s = "01001011" ;
let ans = minOperation(s, n);
document.write(ans, "</br>" );
</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++
#include <bits/stdc++.h>
using namespace std;
int minOperation(string s, int n)
{
int ans = 0;
int i = 0, j = n - 1;
while (i < j) {
if (s[i] == '0' && s[j] == '1' ) {
ans++;
i++;
j--;
continue ;
}
if (s[i] == '1' ) {
i++;
}
if (s[j] == '0' ) {
j--;
}
}
return ans;
}
int main()
{
int n = 8;
string s = "10100101" ;
int ans = minOperation(s, n);
cout << ans << endl;
}
|
Java
import java.util.*;
class GFG{
static int minOperation(String s, int n)
{
int ans = 0 ;
int i = 0 , j = n - 1 ;
while (i < j)
{
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 ans;
}
public static void main (String[] args)
{
int n = 8 ;
String s = "10100101" ;
System.out.println(minOperation(s, n));
}
}
|
Python3
def minOperation(s, n):
ans = 0 ;
i = 0 ; j = n - 1 ;
while (i < j):
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 ans;
if __name__ = = '__main__' :
n = 8 ;
s = "10100101" ;
print (minOperation(s, n));
|
C#
using System;
class GFG{
static int minOperation(String s, int n)
{
int ans = 0;
int i = 0, j = n - 1;
while (i < j)
{
if (s[i] == '0' &&
s[j] == '1' )
{
ans++;
i++;
j--;
continue ;
}
if (s[i] == '1' )
{
i++;
}
if (s[j] == '0' )
{
j--;
}
}
return ans;
}
public static void Main(String[] args)
{
int n = 8;
String s = "10100101" ;
Console.WriteLine(minOperation(s, n));
}
}
|
Javascript
<script>
function minOperation(s, n)
{
let ans = 0;
let i = 0, j = n - 1;
while (i < j)
{
if (s[i] == '0' &&
s[j] == '1' )
{
ans++;
i++;
j--;
continue ;
}
if (s[i]== '1' )
{
i++;
}
if (s[j] == '0' )
{
j--;
}
}
return ans;
}
let n = 8;
let s = "10100101" ;
document.write(minOperation(s, n));
</script>
|
Output:
2
Time Complexity: O(N)
Auxiliary Space: O(1)