Minimum flips required in a binary string such that all K-size substring contains 1
Given a binary string str of size N and a positive integer K, the task is to find the minimum number of flips required to make all substring of size K contain at least one ‘1’.
Examples:
Input: str = “0001”, K = 2
Output: 1
Explanation:
Flipping the bit at index 1 modifies str to “0101”.
All substrings of size 2 are “01”, “10”, and “01”.
Each substring contains at least one 1.
Input: str = “101”, K = 2
Output: 0
Explanation:
All substrings of size 2 are “10” and “01”.
Since both of them already have at least one ‘1’, no flips required in the original string.
Approach:
Follow the steps below to solve the problem:
- The idea is use Sliding Window Technique to check whether every substring of length K contains any ‘1’ or not.
- Maintain a variable last_idx to store the last index of a window where the character was ‘1’. The value of this variable will be -1 if there is no ‘1’ present in the current window.
- For any such window, we will increment the number of flips by flipping the character at last index of the current window to ‘1’ and update the index last_idx to that index.
- Flipping the last character of the current window ensures that the following K-1 windows will have at least one ‘1’ as well. Thus, this approach minimizes the number of flips required.
- Repeat this process for the rest of the string and print the final count of flips required.
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
int minimumMoves(string S, int K)
{
int N = S.length();
int ops = 0;
int last_idx = -1;
for ( int i = 0; i < K; i++) {
if (S[i] == '1' )
last_idx = i;
}
if (last_idx == -1) {
++ops;
S[K - 1] = '1' ;
last_idx = K - 1;
}
for ( int i = 1; i < N - K + 1; i++) {
if (last_idx < i)
last_idx = -1;
if (S[i + K - 1] == '1' )
last_idx = i + K - 1;
if (last_idx == -1) {
++ops;
S[i + K - 1] = '1' ;
last_idx = i + K - 1;
}
}
return ops;
}
int main()
{
string S = "001010000" ;
int K = 3;
cout << minimumMoves(S, K);
return 0;
}
|
Java
class GFG{
public static int minimumMoves(String s, int K)
{
StringBuilder S = new StringBuilder(s);
int N = S.length();
int ops = 0 ;
int last_idx = - 1 ;
for ( int i = 0 ; i < K; i++)
{
if (S.charAt(i) == '1' )
last_idx = i;
}
if (last_idx == - 1 )
{
++ops;
S.setCharAt(K - 1 , '1' );
last_idx = K - 1 ;
}
for ( int i = 1 ; i < N - K + 1 ; i++)
{
if (last_idx < i)
last_idx = - 1 ;
if (S.charAt(i + K - 1 ) == '1' )
last_idx = i + K - 1 ;
if (last_idx == - 1 )
{
++ops;
S.setCharAt(i + K - 1 , '1' );
last_idx = i + K - 1 ;
}
}
return ops;
}
public static void main(String[] args)
{
String S = "001010000" ;
int K = 3 ;
System.out.println(minimumMoves(S, K));
}
}
|
Python3
def minimumMoves(S, K):
N = len (S)
ops = 0
last_idx = - 1
for i in range (K):
if (S[i] = = '1' ):
last_idx = i
if (last_idx = = - 1 ):
ops + = 1
S[K - 1 ] = '1'
last_idx = K - 1
for i in range (N - K + 1 ):
if (last_idx < i):
last_idx = - 1
if (S[i + K - 1 ] = = '1' ):
last_idx = i + K - 1
if (last_idx = = - 1 ):
ops + = 1
S = S[:i + K - 1 ] + '1' + S[i + K:]
last_idx = i + K - 1
return ops
S = "001010000"
K = 3 ;
print (minimumMoves(S, K))
|
C#
using System;
using System.Text;
class GFG{
public static int minimumMoves(String s, int K)
{
StringBuilder S = new StringBuilder(s);
int N = S.Length;
int ops = 0;
int last_idx = -1;
for ( int i = 0; i < K; i++)
{
if (S[i] == '1' )
last_idx = i;
}
if (last_idx == -1)
{
++ops;
S.Insert(K - 1, '1' );
last_idx = K - 1;
}
for ( int i = 1; i < N - K + 1; i++)
{
if (last_idx < i)
last_idx = -1;
if (S[i + K - 1] == '1' )
last_idx = i + K - 1;
if (last_idx == -1)
{
++ops;
S.Insert(i + K - 1, '1' );
last_idx = i + K - 1;
}
}
return ops;
}
public static void Main(String[] args)
{
String S = "001010000" ;
int K = 3;
Console.WriteLine(minimumMoves(S, K));
}
}
|
Javascript
<script>
function minimumMoves(S, K)
{
var N = S.length;
var ops = 0;
var last_idx = -1;
for ( var i = 0; i < K; i++) {
if (S[i] == '1' )
last_idx = i;
}
if (last_idx == -1) {
++ops;
S[K - 1] = '1' ;
last_idx = K - 1;
}
for ( var i = 1; i < N - K + 1; i++) {
if (last_idx < i)
last_idx = -1;
if (S[i + K - 1] == '1' )
last_idx = i + K - 1;
if (last_idx == -1) {
++ops;
S[i + K - 1] = '1' ;
last_idx = i + K - 1;
}
}
return ops;
}
var S = "001010000" ;
var K = 3;
document.write( minimumMoves(S, K));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
26 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...