Count of 0s to be flipped to make any two adjacent 1s at least K 0s apart

• Last Updated : 27 Apr, 2021

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

 // C++ program for the above problem#include using namespace std; // Function to find the// count of 0s to be flippedint 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 codeint main(){    int K = 2;    string s = "000000";     cout << count(K, s) << endl;     return 0;} // This code is contributed by divyeshrabadiya07

Java

 // Java program for the above problemimport java.util.Scanner; // Driver Codepublic 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));    }}

Python3

 # Python3 program for the above problem # Function to find the # count of 0s to be flippeddef count(k, s):         ar =  * len(s)    end = 0     # Loop traversal to mark K     # adjacent positions to the right     # of already existing 1s.    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     # Loop traversal to mark K     # adjacent positions to the left     # of already existing 1s.    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     # Loop to count the maximum     # number of 0s that will be     # replaced by 1s    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 # Driver codeK = 2s = "000000" print(count(K, s)) # This code is contributed by avanitrachhadiya2155

C#

 // C# program for the above problemusing System; class GFG{ // Function to find the// count of 0s to be flippedpublic 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 codepublic static void Main(String[] args){    int K = 2;    String s = "000000";     Console.WriteLine(count(K, s));}} // This code is contributed by amal kumar choubey

Javascript


Output:
2

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

My Personal Notes arrow_drop_up