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:
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:
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: 



  1. Insert the all the characters of the given string in an array(say arr[]).
  2. 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.
  3. Initialize a count variable to 0.
  4. 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.
  5. Convert all the 0s to -1 which are located at <= K places near the newly converted 1.
  6. 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above problem 
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the 
// count of 0s to be flipped 
int count(int k, string s) 
    int ar[s.length()]; 
    int end = 0; 
      
    // Loop traversal to mark K 
    // adjacent positions to the right 
    // of already existing 1s. 
    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; 
      
    // Loop traversal to mark K 
    // adjacent positions to the left 
    // of already existing 1s. 
    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; 
      
    // Loop to count the maximum 
    // number of 0s that will be 
    // replaced by 1s 
    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; 
}
  
// Driver code
int main()
{
    int K = 2; 
    string s = "000000"
  
    cout << count(K, s) << endl;
  
    return 0;
}
  
// This code is contributed by divyeshrabadiya07

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above problem
import java.util.Scanner;
  
// Driver Code
public class Check {
  
    // Function to find the
    // count of 0s to be flipped
    public static int count(int k, String s)
    {
  
        int ar[] = new int[s.length()];
        int end = 0;
  
        // Loop traversal to mark K
        // adjacent positions to the right
        // of already existing 1s.
        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;
  
        // Loop traversal to mark K
        // adjacent positions to the left
        // of already existing 1s.
        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;
  
        // Loop to count the maximum
        // number of 0s that will be
        // replaced by 1s
        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;
    }
  
    // Driver code
    public static void main(String[] args)
    {
  
        int K = 2;
        String s = "000000";
  
        System.out.println(count(K, s));
    }
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above problem
using System;
  
class GFG{
  
// Function to find the
// count of 0s to be flipped
public static int count(int k, String s)
{
  
    int []ar = new int[s.Length];
    int end = 0;
  
    // Loop traversal to mark K
    // adjacent positions to the right
    // of already existing 1s.
    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;
      
    // Loop traversal to mark K
    // adjacent positions to the left
    // of already existing 1s.
    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;
  
    // Loop to count the maximum
    // number of 0s that will be
    // replaced by 1s
    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;
}
  
// Driver code
public static void Main(String[] args)
{
    int K = 2;
    String s = "000000";
  
    Console.WriteLine(count(K, s));
}
}
  
// This code is contributed by amal kumar choubey

chevron_right


Output:

2

Time Complexity: O(N * K) 
Auxillary Space: O(1)
 

competitive-programming-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.