Open In App

Maximize 0s to be flipped in given Binary array such that there are at least K 0s between two 1s

Last Updated : 15 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Given a binary array arr[] and an integer K, the task is to count the maximum number of 0’s that can be flipped to 1’s such that there are at least K 0’s between two 1’s.

Example:

Input: arr[] = {0, 0, 1, 0, 0, 0}, K = 1
Output: 2
Explanation: The 1st and the 5th index in the array arr[] can be flipped such that there is atleast 1 zero between any two 1’s. Therefore, the array after flipping is arr[] = {1, 0, 1, 0, 1, 0}.

Input: arr[] = {1, 0, 0, 0, 0, 0, 0, 0, 1, 0}, K = 2
Output: 1
Explanation: The 4th index in the above array is the only valid index that can be flipped

 

Approach: The given problem can be solved by iterating through the array and finding the count of consecutive zeroes between the two 1’s. Suppose, the number of 0’s between two 1’s is X. Then, it can be observed that the number of 0’s that can be flipped in between are (X-K) / (K+1). Therefore, traverse the array and keep track of the number of consecutive 0’s between two 1’s similar to the algorithm discussed here and add the count of 0’s that can be flipped into a variable cnt, which is the required answer.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the maximum number
// of 1s that can be placed in array arr[]
int maximumOnes(int arr[], int N, int K)
{
 
    // Stores the count of 1's
    int cnt = 0;
 
    // Stores the last index of 1
    int last = -(K + 1);
 
    // Loop to iterate through the array
    for (int i = 0; i < N; i++) {
 
        // If the current element is 1
        if (arr[i] == 1) {
 
            // Check if there are sufficient
            // 0's between consecutive 1's to
            // insert more 1's between them
            if (i - last - 1 >= 2 * (K - 1)) {
                cnt += (i - last - 1 - K) / (K + 1);
            }
 
            // Update the index of last 1
            last = i;
        }
    }
 
    // Condition to include the segment of
    // 0's in the last
    cnt += (N - last - 1) / (K + 1);
 
    // Return answer
    return cnt;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int K = 2;
 
    cout << maximumOnes(arr, N, K);
 
    return 0;
}


Java




// Java program for the above approach
import java.io.*;
class GFG
{
 
    // Function to find the maximum number
    // of 1s that can be placed in array arr[]
    static int maximumOnes(int arr[], int N, int K)
    {
 
        // Stores the count of 1's
        int cnt = 0;
 
        // Stores the last index of 1
        int last = -(K + 1);
 
        // Loop to iterate through the array
        for (int i = 0; i < N; i++) {
 
            // If the current element is 1
            if (arr[i] == 1) {
 
                // Check if there are sufficient
                // 0's between consecutive 1's to
                // insert more 1's between them
                if (i - last - 1 >= 2 * (K - 1)) {
                    cnt += (i - last - 1 - K) / (K + 1);
                }
 
                // Update the index of last 1
                last = i;
            }
        }
 
        // Condition to include the segment of
        // 0's in the last
        cnt += (N - last - 1) / (K + 1);
 
        // Return answer
        return cnt;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0 };
        int N = arr.length;
        int K = 2;
 
        System.out.println(maximumOnes(arr, N, K));
    }
}
 
// This code is contributed by Potta Lokesh


Python3




# Python3 program for the above approach
 
# Function to find the maximum number
# of 1s that can be placed in array arr[]
def maximumOnes(arr, N, K) :
 
    # Stores the count of 1's
    cnt = 0;
 
    # Stores the last index of 1
    last = -(K + 1);
 
    # Loop to iterate through the array
    for i in range(N) :
 
        # If the current element is 1
        if (arr[i] == 1) :
 
            # Check if there are sufficient
            # 0's between consecutive 1's to
            # insert more 1's between them
            if (i - last - 1 >= 2 * (K - 1)) :
                cnt += (i - last - 1 - K) // (K + 1);
            # Update the index of last 1
            last = i;
 
    # Condition to include the segment of
    # 0's in the last
    cnt += (N - last - 1) // (K + 1);
 
    # Return answer
    return cnt;
 
# Driver Code
if __name__ == "__main__" :
 
    arr = [ 1, 0, 0, 0, 0, 0, 0, 0, 1, 0 ];
    N = len(arr);
    K = 2;
 
    print(maximumOnes(arr, N, K));
 
    # This code is contributed by AnkThon


C#




// C# program for the above approach
using System;
public class GFG
{
 
    // Function to find the maximum number
    // of 1s that can be placed in array arr[]
    static int maximumOnes(int []arr, int N, int K)
    {
 
        // Stores the count of 1's
        int cnt = 0;
 
        // Stores the last index of 1
        int last = -(K + 1);
 
        // Loop to iterate through the array
        for (int i = 0; i < N; i++) {
 
            // If the current element is 1
            if (arr[i] == 1) {
 
                // Check if there are sufficient
                // 0's between consecutive 1's to
                // insert more 1's between them
                if (i - last - 1 >= 2 * (K - 1)) {
                    cnt += (i - last - 1 - K) / (K + 1);
                }
 
                // Update the index of last 1
                last = i;
            }
        }
 
        // Condition to include the segment of
        // 0's in the last
        cnt += (N - last - 1) / (K + 1);
 
        // Return answer
        return cnt;
    }
 
    // Driver Code
    public static void Main(string[] args)
    {
        int []arr = { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0 };
        int N = arr.Length;
        int K = 2;
 
        Console.WriteLine(maximumOnes(arr, N, K));
    }
}
 
// This code is contributed by AnkThon


Javascript




<script>
 
// Javascript program for the above approach
 
// Function to find the maximum number
// of 1s that can be placed in array arr[]
function maximumOnes(arr, N, K)
{
 
    // Stores the count of 1's
    var cnt = 0;
 
    // Stores the last index of 1
    var last = -(K + 1);
 
    // Loop to iterate through the array
    for (var i = 0; i < N; i++) {
 
        // If the current element is 1
        if (arr[i] == 1) {
 
            // Check if there are sufficient
            // 0's between consecutive 1's to
            // insert more 1's between them
            if (i - last - 1 >= 2 * (K - 1)) {
                cnt += parseInt((i - last - 1 - K) / (K + 1));
            }
 
            // Update the index of last 1
            last = i;
        }
    }
 
    // Condition to include the segment of
    // 0's in the last
    cnt += parseInt((N - last - 1) / (K + 1));
 
    // Return answer
    return cnt;
}
 
// Driver Code
var arr = [1, 0, 0, 0, 0, 0, 0, 0, 1, 0 ];
var N = arr.length;
var K = 2;
document.write(maximumOnes(arr, N, K));
 
// This code is contributed by rutvik_56.
</script>


Output

1

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads