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
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++
#include <bits/stdc++.h>
using namespace std;
int maximumOnes( int arr[], int N, int K)
{
int cnt = 0;
int last = -(K + 1);
for ( int i = 0; i < N; i++) {
if (arr[i] == 1) {
if (i - last - 1 >= 2 * (K - 1)) {
cnt += (i - last - 1 - K) / (K + 1);
}
last = i;
}
}
cnt += (N - last - 1) / (K + 1);
return cnt;
}
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
import java.io.*;
class GFG
{
static int maximumOnes( int arr[], int N, int K)
{
int cnt = 0 ;
int last = -(K + 1 );
for ( int i = 0 ; i < N; i++) {
if (arr[i] == 1 ) {
if (i - last - 1 >= 2 * (K - 1 )) {
cnt += (i - last - 1 - K) / (K + 1 );
}
last = i;
}
}
cnt += (N - last - 1 ) / (K + 1 );
return cnt;
}
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));
}
}
|
Python3
def maximumOnes(arr, N, K) :
cnt = 0 ;
last = - (K + 1 );
for i in range (N) :
if (arr[i] = = 1 ) :
if (i - last - 1 > = 2 * (K - 1 )) :
cnt + = (i - last - 1 - K) / / (K + 1 );
last = i;
cnt + = (N - last - 1 ) / / (K + 1 );
return cnt;
if __name__ = = "__main__" :
arr = [ 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 ];
N = len (arr);
K = 2 ;
print (maximumOnes(arr, N, K));
|
C#
using System;
public class GFG
{
static int maximumOnes( int []arr, int N, int K)
{
int cnt = 0;
int last = -(K + 1);
for ( int i = 0; i < N; i++) {
if (arr[i] == 1) {
if (i - last - 1 >= 2 * (K - 1)) {
cnt += (i - last - 1 - K) / (K + 1);
}
last = i;
}
}
cnt += (N - last - 1) / (K + 1);
return cnt;
}
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));
}
}
|
Javascript
<script>
function maximumOnes(arr, N, K)
{
var cnt = 0;
var last = -(K + 1);
for ( var i = 0; i < N; i++) {
if (arr[i] == 1) {
if (i - last - 1 >= 2 * (K - 1)) {
cnt += parseInt((i - last - 1 - K) / (K + 1));
}
last = i;
}
}
cnt += parseInt((N - last - 1) / (K + 1));
return cnt;
}
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));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...