Minimum bit flips such that every K consecutive bits contain at least one set bit
Given a binary string S, and an integer K, the task is to find the minimum number of flips required such that every substring of length K contains at least one ‘1’.
Examples:
Input: S = “10000001” K = 2
Output: 3
Explanation:
We need only 3 changes in string S ( at position 2, 4 and 6 ) so that the string contain at least one ‘1’ in every sub-string of length 2.
Input: S = “000000” K = 3
Output: 2
Explanation:
We need only 3 changes in string S ( at position 2 and 5 ) so that the string contain at least one ‘1’ in every sub-string of length 3.
Input: S = “111010111” K = 2
Output: 0
Naive Approach:
To solve the problem, the simplest approach is to iterate for each substring of length K and find the minimum number of flips required to satisfy the given condition.
Time complexity: O(N * K)
Space Complexity: O(1)
Efficient Approach:
The idea is to use Sliding Window Approach.
- Set a window size of K.
- Store the index of previous appearance of 1.
- If the current bit is unset and the difference between the current ith bit and the previous set bit exceeds K, set the current bit and store the current index as that of the previous set bit and proceed further.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int CountMinFlips(string s, int n,
int k)
{
int cnt = 0;
int prev = -1;
for ( int i = 0; i < k; i++) {
if (s[i] == '1' ) {
prev = i;
}
}
if (prev == -1) {
cnt++;
s[k - 1] = '1' ;
prev = k - 1;
}
for ( int i = k; i < n; i++) {
if (s[i] != '1' ) {
if (prev <= (i - k)) {
s[i] = '1' ;
prev = i;
cnt++;
}
}
else {
prev = i;
}
}
return (cnt);
}
int main()
{
string str = "10000001" ;
int n = str.size();
int k = 2;
cout << CountMinFlips(str, n, k)
<< endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int CountMinFlips( char []s, int n,
int k)
{
int cnt = 0 ;
int prev = - 1 ;
for ( int i = 0 ; i < k; i++)
{
if (s[i] == '1' )
{
prev = i;
}
}
if (prev == - 1 )
{
cnt++;
s[k - 1 ] = '1' ;
prev = k - 1 ;
}
for ( int i = k; i < n; i++)
{
if (s[i] != '1' )
{
if (prev <= (i - k))
{
s[i] = '1' ;
prev = i;
cnt++;
}
}
else
{
prev = i;
}
}
return (cnt);
}
public static void main(String[] args)
{
String str = "10000001" ;
int n = str.length();
int k = 2 ;
System.out.print(CountMinFlips(
str.toCharArray(), n, k) + "\n" );
}
}
|
Python3
def CountMinFlips(s, n, k):
cnt = 0
prev = - 1
for i in range ( 0 , k):
if (s[i] = = '1' ):
prev = i;
if (prev = = - 1 ):
cnt + = 1
prev = k - 1 ;
for i in range (k, n):
if (s[i] ! = '1' ):
if ( prev < = (i - k) ):
prev = i;
cnt + = 1
else :
prev = i
return (cnt);
s = "10000001"
n = len (s)
k = 2
print (CountMinFlips(s, n, k))
|
C#
using System;
class GFG{
static int CountMinFlips( char []s, int n,
int k)
{
int cnt = 0;
int prev = -1;
for ( int i = 0; i < k; i++)
{
if (s[i] == '1' )
{
prev = i;
}
}
if (prev == -1)
{
cnt++;
s[k - 1] = '1' ;
prev = k - 1;
}
for ( int i = k; i < n; i++)
{
if (s[i] != '1' )
{
if (prev <= (i - k))
{
s[i] = '1' ;
prev = i;
cnt++;
}
}
else
{
prev = i;
}
}
return (cnt);
}
public static void Main(String[] args)
{
String str = "10000001" ;
int n = str.Length;
int k = 2;
Console.Write(CountMinFlips(
str.ToCharArray(), n, k) + "\n" );
}
}
|
Javascript
<script>
function CountMinFlips(s , n,k)
{
var cnt = 0;
var prev = -1;
for (i = 0; i < k; i++)
{
if (s[i] == '1' )
{
prev = i;
}
}
if (prev == -1)
{
cnt++;
s[k - 1] = '1' ;
prev = k - 1;
}
for (i = k; i < n; i++)
{
if (s[i] != '1' )
{
if (prev <= (i - k))
{
s[i] = '1';
prev = i;
cnt++;
}
}
else
{
prev = i;
}
}
return (cnt);
}
var str = "10000001" ;
var n = str.length;
var k = 2;
document.write(CountMinFlips(
str, n, k) );
</script>
|
Time complexity: O(N + K)
Auxiliary Space: O(1)
Last Updated :
10 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...