Minimize flips to make binary string as all 1s by flipping characters in substring of size K repeatedly
Last Updated :
29 Sep, 2021
Given a binary string S of size N and an integer K, the task is to find the minimum number of operations required to make all characters as 1s in the binary string by flipping characters in the substring of size K. If it is not possible to do so, then print “-1”.
Examples:
Input: S = “00010110 “, K = 3
Output: 3
Explanation:
Below are the operations performed:
- Consider the substring S[0, 2] which is of size K(= 3) and flipping those characters modifies the string to “11110110”.
- Consider the substring S[4, 6] which is of size K(= 3) and flipping those characters modifies the string to “11111000”.
- Consider the substring S[5, 7] which is of size K(= 3) and flipping those characters modifies the string to “11111111”.
After the above operations, all the 0s are converted to 1s, and the minimum number of operations required is 3.
Input: S = “01010”, K = 4
Output: -1
Approach: The given problem can be solved using the Greedy Approach. Follow the below steps to solve the problem:
- Initialize a variable, say minOperations as 0 that stores the count of the minimum number of operations required.
- Traverse the string S using the variable i and if the characters S[i] is ‘0’ and the value of (i + K) is at most K, then flip all the characters of the substring S[i, i + K] and increment the value of minOperations by 1.
- After the above operations, traverse the string S and if there exists any character ‘0’ then print “-1” and break out of the loop.
- If all the characters of the string S are 1s, then print the minOperations as the resultant minimum number of operations.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void minOperation(string S, int K, int N)
{
int min = 0;
int i;
for (i = 0; i < N; i++) {
if (S[i] == '0' && i + K <= N) {
for ( int j = i; j < i + K; j++) {
if (S[j] == '1' )
S[j] = '0' ;
else
S[j] = '1' ;
}
min++;
}
}
for (i = 0; i < N; i++) {
if (S[i] == '0' )
break ;
}
if (i == N)
cout << min;
else
cout << -1;
}
int main()
{
string S = "00010110" ;
int K = 3;
int N = S.length();
minOperation(S, K, N);
return 0;
}
|
Java
public class GFG
{
static void minOperation(String S, int K, int N)
{
int min = 0 ;
int i;
for (i = 0 ; i < N; i++) {
if (S.charAt(i) == '0' && i + K <= N) {
for ( int j = i; j < i + K; j++) {
if (S.charAt(j) == '1' )
S = S.substring( 0 , j) + '0' + S.substring(j + 1 );
else
S = S.substring( 0 , j) + '1' + S.substring(j + 1 );
}
min++;
}
}
for (i = 0 ; i < N; i++) {
if (S.charAt(i) == '0' )
break ;
}
if (i == N)
System.out.println(min);
else
System.out.println(- 1 );
}
public static void main(String []args)
{
String S = "00010110" ;
int K = 3 ;
int N = S.length();
minOperation(S, K, N);
}
}
|
Python3
def minOperation(St, K, N):
S = list (St)
min = 0
i = 0
for i in range ( 0 , N):
if (S[i] = = '0' and i + K < = N):
j = i
while (j < i + K):
if (S[j] = = '1' ):
S[j] = '0'
else :
S[j] = '1'
j + = 1
min + = 1
temp = 0
for i in range ( 0 , N):
temp + = 1
if (S[i] = = '0' ):
break
if (temp = = N):
print ( min )
else :
print ( - 1 )
S = "00010110"
K = 3
N = len (S)
minOperation(S, K, N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void minOperation( string S, int K, int N)
{
int min = 0;
int i;
for (i = 0; i < N; i++) {
if (S[i] == '0' && i + K <= N) {
for ( int j = i; j < i + K; j++) {
if (S[j] == '1' )
S = S.Substring(0, j) + '0' + S.Substring(j + 1);
else
S = S.Substring(0, j) + '1' + S.Substring(j + 1);
}
min++;
}
}
for (i = 0; i < N; i++) {
if (S[i] == '0' )
break ;
}
if (i == N)
Console.Write(min);
else
Console.Write(-1);
}
public static void Main()
{
string S = "00010110" ;
int K = 3;
int N = S.Length;
minOperation(S, K, N);
}
}
|
Javascript
<script>
function minOperation(St, K, N)
{
S = St.split( '' );
let min = 0;
let i;
for (i = 0; i < N; i++) {
if (S[i] == '0' && i + K <= N) {
for (let j = i; j < i + K; j++) {
if (S[j] == '1' )
S[j] = '0' ;
else
S[j] = '1' ;
}
min++;
}
}
for (i = 0; i < N; i++) {
if (S[i] == '0' )
break ;
}
if (i == N)
document.write(min);
else
document.write(-1);
}
let S = "00010110" ;
let K = 3;
let N = S.length;
minOperation(S, K, N);
</script>
|
Time Complexity: O(N*K)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...