Count of 0s to be flipped to make any two adjacent 1s at least K 0s apart
Given a binary string s and a number K, the task is to find the maximum number of 0s that can be replaced by 1s such that two adjacent 1s are separated by at least K 0s in between them.
Examples:
Input: K = 2, s = “000000”
Output: 2
Explanation:
Change the 0s at position 0 and 3. Then the final string will be “100100” such that every 1 is separated by at least 2 0s.
Input: K = 1, s = “01001000100000”
Output: 3
Explanation:
Change the 0s at position 6, 10, and 12. Then the final string will be “01001010101010” such that every 1 is separated by at least 1 0s.
Approach:
- Insert all the characters of the given string in an array(say arr[]).
- Traverse the array arr[] and convert all the 0s to -1 which are located at <= K places near an already existing 1. This operation gives all the possible positions of the string where 1 can be inserted.
- Initialize a count variable to 0.
- Traverse the array arr[] from the left till the end. As soon as the first 0 is encountered replace it with 1 and increase the count value.
- Convert all the 0s to -1 which are located at <= K places near the newly converted 1.
- Keep traversing the array till the end and every time a 0 is encountered, repeat the steps 4 – 5.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int count( int k, string s)
{
int ar[s.length()];
int end = 0;
for ( int i = 0; i < s.length(); i++)
{
if (s[i] == '1' )
{
for ( int j = i;
j < s.length() &&
j <= i + k; j++)
{
ar[j] = -1;
end = j;
}
i = end;
}
}
end = 0;
for ( int i = s.length() - 1;
i >= 0; i--)
{
if (s[i] == '1' )
{
for ( int j = i;
j >= 0 &&
j >= i - k; j--)
{
ar[j] = -1;
end = j;
}
i = end;
}
}
int ans = 0;
end = 0;
for ( int j = 0;
j < s.length(); j++)
{
if (ar[j] == 0)
{
ans++;
for ( int g = j;
g <= j + k &&
g < s.length(); g++)
{
ar[g] = -1;
end = g;
}
j = end - 1;
}
}
return ans;
}
int main()
{
int K = 2;
string s = "000000" ;
cout << count(K, s) << endl;
return 0;
}
|
Java
import java.util.Scanner;
public class Check {
public static int count( int k, String s)
{
int ar[] = new int [s.length()];
int end = 0 ;
for ( int i = 0 ;
i < s.length(); i++) {
if (s.charAt(i) == '1' ) {
for ( int j = i;
j < s.length()
&& j <= i + k;
j++) {
ar[j] = - 1 ;
end = j;
}
i = end;
}
}
end = 0 ;
for ( int i = s.length() - 1 ;
i >= 0 ; i--) {
if (s.charAt(i) == '1' ) {
for ( int j = i;
j >= 0 && j >= i - k;
j--) {
ar[j] = - 1 ;
end = j;
}
i = end;
}
}
int ans = 0 ;
end = 0 ;
for ( int j = 0 ;
j < s.length(); j++) {
if (ar[j] == 0 ) {
ans++;
for ( int g = j;
g <= j + k
&& g < s.length();
g++) {
ar[g] = - 1 ;
end = g;
}
j = end - 1 ;
}
}
return ans;
}
public static void main(String[] args)
{
int K = 2 ;
String s = "000000" ;
System.out.println(count(K, s));
}
}
|
Python3
def count(k, s):
ar = [ 0 ] * len (s)
end = 0
for i in range ( len (s)):
if s[i] = = '1' :
for j in range (i, len (s)):
if (j < = i + k):
ar[j] = - 1
end = j
i = end
end = 0
for i in range ( len (s) - 1 , - 1 , - 1 ):
if (s[i] = = '1' ):
for j in range (i, - 1 , - 1 ):
if (j > = i - k):
ar[j] = - 1
end = j
i = end
ans = 0
end = 0
for j in range ( len (s)):
if (ar[j] = = 0 ):
ans + = 1
for g in range (j, len (s)):
if (g < = j + k):
ar[g] = - 1
end = g
j = end - 1
return ans
K = 2
s = "000000"
print (count(K, s))
|
C#
using System;
class GFG{
public static int count( int k, String s)
{
int []ar = new int [s.Length];
int end = 0;
for ( int i = 0; i < s.Length; i++)
{
if (s[i] == '1' )
{
for ( int j = i;
j < s.Length &&
j <= i + k; j++)
{
ar[j] = -1;
end = j;
}
i = end;
}
}
end = 0;
for ( int i = s.Length - 1; i >= 0; i--)
{
if (s[i] == '1' )
{
for ( int j = i;
j >= 0 &&
j >= i - k; j--)
{
ar[j] = -1;
end = j;
}
i = end;
}
}
int ans = 0;
end = 0;
for ( int j = 0; j < s.Length; j++)
{
if (ar[j] == 0)
{
ans++;
for ( int g = j;
g <= j + k &&
g < s.Length; g++)
{
ar[g] = -1;
end = g;
}
j = end - 1;
}
}
return ans;
}
public static void Main(String[] args)
{
int K = 2;
String s = "000000" ;
Console.WriteLine(count(K, s));
}
}
|
Javascript
<script>
function count(k, s)
{
var ar = Array(s.length).fill(0);
var end = 0;
var i,j;
for (i = 0; i < s.length; i++)
{
if (s[i] == '1' )
{
for (j = i; j < s.length &&
j <= i + k; j++)
{
ar[j] = -1;
end = j;
}
i = end;
}
}
end = 0;
for (i = s.length - 1; i >= 0; i--)
{
if (s[i] == '1' )
{
for (j = i; j >= 0 &&
j >= i - k; j--)
{
ar[j] = -1;
end = j;
}
i = end;
}
}
var ans = 0;
end = 0;
var g;
for (j = 0;j < s.length; j++)
{
if (ar[j] == 0)
{
ans++;
for (g = j; g <= j + k &&
g < s.length; g++)
{
ar[g] = -1;
end = g;
}
j = end - 1;
}
}
return ans;
}
var K = 2;
var s = "000000" ;
document.write(count(K, s));
</script>
|
Time Complexity: O(N * K)
Auxiliary Space: O(1)
Last Updated :
27 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...