Minimize count of flips required such that no substring of 0s have length exceeding K
Last Updated :
17 May, 2021
Given a binary string str of length N and an integer K where K is in the range (1 ? K ? N), the task is to find the minimum number of flips( conversion of 0s to 1 or vice versa) required to be performed on the given string such that the resulting string does not contain K or more zeros together.
Examples:
Input: str = “11100000011”, K = 3
Output: 2
Explanation:
Flipping 6th and 7th characters modifies the string to “11100110011”.
Therefore, no substring of 0s are present in the string having length 3 or more.
Input: str = “110011”, K = 1
Output: 2
Flip 3rd and 4th characters then str = “111111” which does not contain 1 or more zeros together.
Naive Approach: The simplest approach is to generate all possible substrings of the given string and for each substring, check if it consists of only 0s or not. If found to be true, check if its length exceeds K or not. If found to be true, count the number of flips required for that substring. Finally, print the count of flips obtained.
Time Complexity: O(2N)
Auxiliary Space: O(N)
Efficient Approach: To make the count of contiguous zeros in resultant string less than K, choose the substrings consisting of only zeros of length ? K. So the problem reduces to finding the minimum flips in each substring. The final result will be the sum of minimum flips of all such subsegments. Below are the steps:
- Initialize the result to 0and the count of contiguous zeros (say cnt_zeros) to 0.
- Transverse the string and on encountering a ‘0’ increment cnt_zeros by 1.
- If cnt_zeros becomes equal to K then increment result and set cnt_zeros to 0.
- And when a ‘1’ comes then set cnt_zeros to 0 since the count of contiguous zeros should become 0.
Below the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int min_flips(string& str, int k)
{
if (str.size() == 0)
return 0;
int ans = 0;
int cnt_zeros = 0;
for ( char ch : str) {
if (ch == '0' ) {
++cnt_zeros;
}
else {
cnt_zeros = 0;
}
if (cnt_zeros == k) {
++ans;
cnt_zeros = 0;
}
}
return ans;
}
int main()
{
string str = "11100000011" ;
int k = 3;
cout << min_flips(str, k);
return 0;
}
|
Java
class GFG{
static int min_flips(String str, int k)
{
if (str.length() == 0 )
return 0 ;
int ans = 0 ;
int cnt_zeros = 0 ;
for ( char ch : str.toCharArray())
{
if (ch == '0' )
{
++cnt_zeros;
}
else
{
cnt_zeros = 0 ;
}
if (cnt_zeros == k)
{
++ans;
cnt_zeros = 0 ;
}
}
return ans;
}
public static void main(String[] args)
{
String str = "11100000011" ;
int k = 3 ;
System.out.print(min_flips(str, k));
}
}
|
Python3
def min_flips(strr, k):
if ( len (strr) = = 0 ):
return 0
ans = 0
cnt_zeros = 0
for ch in strr:
if (ch = = '0' ):
cnt_zeros + = 1
else :
cnt_zeros = 0
if (cnt_zeros = = k):
ans + = 1
cnt_zeros = 0
return ans
if __name__ = = '__main__' :
strr = "11100000011"
k = 3
print (min_flips(strr, k))
|
C#
using System;
class GFG{
static int min_flips(String str, int k)
{
if (str.Length == 0)
return 0;
int ans = 0;
int cnt_zeros = 0;
foreach ( char ch in str.ToCharArray())
{
if (ch == '0' )
{
++cnt_zeros;
}
else
{
cnt_zeros = 0;
}
if (cnt_zeros == k)
{
++ans;
cnt_zeros = 0;
}
}
return ans;
}
public static void Main(String[] args)
{
String str = "11100000011" ;
int k = 3;
Console.Write(min_flips(str, k));
}
}
|
Javascript
<script>
function min_flips(str, k)
{
if (str.length == 0)
return 0;
let ans = 0;
let cnt_zeros = 0;
for (let ch in str.split( '' ))
{
if (str[ch] == '0' )
{
++cnt_zeros;
}
else
{
cnt_zeros = 0;
}
if (cnt_zeros == k)
{
++ans;
cnt_zeros = 0;
}
}
return ans;
}
let str = "11100000011" ;
let k = 3;
document.write(min_flips(str, k));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...