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

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 ` `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);` `    ``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

 ``

Output

`1`

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

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next