Maximum number of consecutive 1s after flipping all 0s in a K length subarray
Last Updated :
14 Jan, 2022
Given a binary array arr[] of length N, and an integer K, the task is to find the maximum number of consecutive ones after flipping all zero in a subarray of length K.
Examples:
Input: arr[]= {0, 0, 1, 1, 1, 1, 0, 1, 1, 0}, K = 2
Output: 7
Explanation:
On taking the subarray [6, 7] and flip zero to one we get 7 consecutive ones.
Input: arr[]= {0, 0, 1, 1, 0, 0, 0, 0}, K = 3
Output: 5
Explanation:
On taking the subarray [4, 6] and flip zero to one we get 5 consecutive ones.
Approach: To solve the problem follow the steps given below:
- Initialize a variable let’s say trav which is going to iterate in the array from each position i to (0 to i-1) in the left direction and from (i+k to n-1) in the right direction.
- Check and keep an account that no zero comes in its way in any direction while iterating in the array.
- If there is a 0 then break out from the loop in that direction.
- So ultimately for i to i+k if there is any zero we are already flipping it to 1 so no need to count number of ones in this range as it will be equal to integer K only.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findmax( int arr[], int n, int k)
{
int trav, i;
int c = 0, maximum = 0;
for (i = 0; i < n - k + 1; i++) {
trav = i - 1;
c = 0;
while (trav >= 0 && arr[trav] == 1) {
trav--;
c++;
}
trav = i + k;
while (trav < n && arr[trav] == 1) {
trav++;
c++;
}
c += k;
if (c > maximum)
maximum = c;
}
return maximum;
}
int main()
{
int k = 3;
int arr[] = { 0, 0, 1, 1, 0, 0, 0, 0 };
int n = sizeof arr / sizeof arr[0];
int ans = findmax(arr, n, k);
cout << ans << '\n' ;
}
|
Java
import java.util.*;
class GFG{
static int findmax( int arr[], int n, int k)
{
int trav, i;
int c = 0 , maximum = 0 ;
for (i = 0 ; i < n - k + 1 ; i++)
{
trav = i - 1 ;
c = 0 ;
while (trav >= 0 && arr[trav] == 1 )
{
trav--;
c++;
}
trav = i + k;
while (trav < n && arr[trav] == 1 )
{
trav++;
c++;
}
c += k;
if (c > maximum)
maximum = c;
}
return maximum;
}
public static void main(String args[])
{
int k = 3 ;
int arr[] = { 0 , 0 , 1 , 1 , 0 , 0 , 0 , 0 };
int n = arr.length;
int ans = findmax(arr, n, k);
System.out.println(ans);
}
}
|
Python3
def findmax(arr, n, k):
trav, i = 0 , 0
c = 0
maximum = 0
while i < n - k + 1 :
trav = i - 1
c = 0
while trav > = 0 and arr[trav] = = 1 :
trav - = 1
c + = 1
trav = i + k
while (trav < n and arr[trav] = = 1 ):
trav + = 1
c + = 1
c + = k
if (c > maximum):
maximum = c
i + = 1
return maximum
if __name__ = = '__main__' :
k = 3
arr = [ 0 , 0 , 1 , 1 , 0 , 0 , 0 , 0 ]
n = len (arr)
ans = findmax(arr, n, k)
print (ans)
|
C#
using System;
class GFG{
static int findmax( int []arr, int n, int k)
{
int trav, i;
int c = 0, maximum = 0;
for (i = 0; i < n - k + 1; i++)
{
trav = i - 1;
c = 0;
while (trav >= 0 && arr[trav] == 1)
{
trav--;
c++;
}
trav = i + k;
while (trav < n && arr[trav] == 1)
{
trav++;
c++;
}
c += k;
if (c > maximum)
maximum = c;
}
return maximum;
}
public static void Main()
{
int k = 3;
int []arr = { 0, 0, 1, 1, 0, 0, 0, 0 };
int n = arr.Length;
int ans = findmax(arr, n, k);
Console.WriteLine(ans);
}
}
|
Javascript
<script>
function findmax(arr, n, k) {
let trav, i;
let c = 0, maximum = 0;
for (i = 0; i < n - k + 1; i++) {
trav = i - 1;
c = 0;
while (trav >= 0 && arr[trav] == 1) {
trav--;
c++;
}
trav = i + k;
while (trav < n && arr[trav] == 1) {
trav++;
c++;
}
c += k;
if (c > maximum)
maximum = c;
}
return maximum;
}
let k = 3;
let arr = [0, 0, 1, 1, 0, 0, 0, 0];
let n = arr.length;
let ans = findmax(arr, n, k);
document.write(ans)
</script>
|
Time complexity: O(N2)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...