Minimum number of swaps required such that a given substring consists of exactly K 1s
Last Updated :
29 Oct, 2021
Given a binary string S of size N and three positive integers L, R, and K, the task is to find the minimum number of swaps required to such that the substring {S[L], .. S[R]} consists of exactly K 1s. If it is not possible to do so, then print “-1”.
Examples:
Input: S = “110011111000101”, L = 5, R = 8, K = 2
Output: 2
Explanation:
Initially, the substring {S[5], .. S[8]} = “1111” consists of 4 1s.
Swap (S[5], S[3]) and (S[6], S[4]).
Modified string S = “111100111000101” and {S[5], .. S[8]} = “0011”.
Therefore, 2 swaps are required.
Input: S = “01010101010101”, L = 3, R = 7, K = 8
Output: -1
Approach: The idea is to count the number of 1s and 0s present in the substring and outside the substring, {S[L], …, S[R]}. Then, check if enough 1s or 0s are present outside that range which can be swapped such that the substring contains exactly K 1s.
Follow the steps below to solve the problem:
- Store the frequency of 1s and 0s in the string S in cntOnes and cntZeros respectively.
- Also, store the frequency of 1s and 0s in the substring S[L, R] in ones and zeros respectively.
- Find the frequency of 1s and 0s outside the substring S[L, R] using the formula: (rem_ones = cntOnes – ones) and rem_zero = (cntZeros – zeros).
- If k ? ones, then do the following:
- Initialize rem = (K – ones), which denotes the number of 1s required to make the sum equal to K.
- If zeros ? rem and rem_ones ? rem, print the value of rem as the result.
- Otherwise, if K < ones, then do the following:
- Initialize rem = (ones – K), which denotes the number of 0s required to make the sum equal to K.
- If ones ? rem and rem_zeros ? rem, print the value of rem as the result.
- In all other cases, print -1.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int minimumSwaps(string s, int l, int r, int k)
{
int n = s.length();
int tot_ones = 0, tot_zeros = 0;
for ( int i = 0; i < s.length(); i++)
{
if (s[i] == '1' )
tot_ones++;
else
tot_zeros++;
}
int ones = 0, zeros = 0, sum = 0;
for ( int i = l - 1; i < r; i++)
{
if (s[i] == '1' )
{
ones++;
sum++;
}
else
zeros++;
}
int rem_ones = tot_ones - ones;
int rem_zeros = tot_zeros - zeros;
if (k >= sum)
{
int rem = k - sum;
if (zeros >= rem && rem_ones >= rem)
return rem;
}
else if (k < sum)
{
int rem = sum - k;
if (ones >= rem && rem_zeros >= rem)
return rem;
}
return -1;
}
int main()
{
string S = "110011111000101" ;
int L = 5, R = 8, K = 2;
cout<<minimumSwaps(S, L, R, K);
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static int minimumSwaps(
String s, int l, int r, int k)
{
int n = s.length();
int tot_ones = 0 , tot_zeros = 0 ;
for ( int i = 0 ; i < s.length(); i++) {
if (s.charAt(i) == '1' )
tot_ones++;
else
tot_zeros++;
}
int ones = 0 , zeros = 0 , sum = 0 ;
for ( int i = l - 1 ; i < r; i++) {
if (s.charAt(i) == '1' ) {
ones++;
sum++;
}
else
zeros++;
}
int rem_ones = tot_ones - ones;
int rem_zeros = tot_zeros - zeros;
if (k >= sum) {
int rem = k - sum;
if (zeros >= rem && rem_ones >= rem)
return rem;
}
else if (k < sum) {
int rem = sum - k;
if (ones >= rem && rem_zeros >= rem)
return rem;
}
return - 1 ;
}
public static void main(String[] args)
{
String S = "110011111000101" ;
int L = 5 , R = 8 , K = 2 ;
System.out.println(
minimumSwaps(S, L, R, K));
}
}
|
Python3
def minimumSwaps(s, l, r, k) :
n = len (s)
tot_ones, tot_zeros = 0 , 0
for i in range ( 0 , len (s)) :
if (s[i] = = '1' ) :
tot_ones + = 1
else :
tot_zeros + = 1
ones, zeros, Sum = 0 , 0 , 0
for i in range (l - 1 , r) :
if (s[i] = = '1' ) :
ones + = 1
Sum + = 1
else :
zeros + = 1
rem_ones = tot_ones - ones
rem_zeros = tot_zeros - zeros
if (k > = Sum ) :
rem = k - Sum
if (zeros > = rem and rem_ones > = rem) :
return rem
elif (k < Sum ) :
rem = Sum - k
if (ones > = rem and rem_zeros > = rem) :
return rem
return - 1
S = "110011111000101"
L, R, K = 5 , 8 , 2
print (minimumSwaps(S, L, R, K))
|
C#
using System;
class GFG
{
static int minimumSwaps( string s, int l, int r, int k)
{
int n = s.Length;
int tot_ones = 0, tot_zeros = 0;
for ( int i = 0; i < s.Length; i++)
{
if (s[i] == '1' )
tot_ones++;
else
tot_zeros++;
}
int ones = 0, zeros = 0, sum = 0;
for ( int i = l - 1; i < r; i++)
{
if (s[i] == '1' )
{
ones++;
sum++;
}
else
zeros++;
}
int rem_ones = tot_ones - ones;
int rem_zeros = tot_zeros - zeros;
if (k >= sum)
{
int rem = k - sum;
if (zeros >= rem && rem_ones >= rem)
return rem;
}
else if (k < sum)
{
int rem = sum - k;
if (ones >= rem && rem_zeros >= rem)
return rem;
}
return -1;
}
public static void Main(String[] args)
{
string S = "110011111000101" ;
int L = 5, R = 8, K = 2;
Console.Write(minimumSwaps(S, L, R, K));
}
}
|
Javascript
<script>
function minimumSwaps(s , l , r , k)
{
var n = s.length;
var tot_ones = 0, tot_zeros = 0;
for (i = 0; i < s.length; i++) {
if (s.charAt(i) == '1' )
tot_ones++;
else
tot_zeros++;
}
var ones = 0, zeros = 0, sum = 0;
for ( var i = l - 1; i < r; i++) {
if (s.charAt(i) == '1' ) {
ones++;
sum++;
}
else
zeros++;
}
var rem_ones = tot_ones - ones;
var rem_zeros = tot_zeros - zeros;
if (k >= sum) {
var rem = k - sum;
if (zeros >= rem && rem_ones >= rem)
return rem;
}
else if (k < sum) {
var rem = sum - k;
if (ones >= rem && rem_zeros >= rem)
return rem;
}
return -1;
}
var S = "110011111000101" ;
var L = 5, R = 8, K = 2;
document.write(
minimumSwaps(S, L, R, K));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...