Open In App

Maximum number of consecutive 1s after flipping all 0s in a K length subarray

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

Below is the implementation of the above approach:




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the maximum number of
// consecutive 1's after flipping all
// zero in a K length subarray
int findmax(int arr[], int n, int k)
{
    // Initialize variable
    int trav, i;
    int c = 0, maximum = 0;
 
    // Iterate until n-k+1 as we
    // have to go till i+k
    for (i = 0; i < n - k + 1; i++) {
        trav = i - 1;
        c = 0;
 
        /*Iterate in the array in left direction
        till you get 1 else break*/
        while (trav >= 0 && arr[trav] == 1) {
            trav--;
            c++;
        }
        trav = i + k;
 
        /*Iterate in the array in right direction
        till you get 1 else break*/
        while (trav < n && arr[trav] == 1) {
            trav++;
            c++;
        }
        c += k;
 
        // Compute the maximum length
        if (c > maximum)
            maximum = c;
    }
 
    // Return the length
    return maximum;
}
 
// Driver code
int main()
{
    int k = 3;
    // Array initialization
    int arr[] = { 0, 0, 1, 1, 0, 0, 0, 0 };
 
    // Size of array
    int n = sizeof arr / sizeof arr[0];
    int ans = findmax(arr, n, k);
    cout << ans << '\n';
}




// Java program for the above approach
import java.util.*;
 
class GFG{
     
// Function to find the maximum number of
// consecutive 1's after flipping all
// zero in a K length subarray
static int findmax(int arr[], int n, int k)
{
     
    // Initialize variable
    int trav, i;
    int c = 0, maximum = 0;
     
    // Iterate until n-k+1 as we
    // have to go till i+k
    for(i = 0; i < n - k + 1; i++)
    {
        trav = i - 1;
        c = 0;
     
        // Iterate in the array in left direction
        // till you get 1 else break
        while (trav >= 0 && arr[trav] == 1)
        {
            trav--;
            c++;
        }
        trav = i + k;
     
        // Iterate in the array in right direction
        // till you get 1 else break
        while (trav < n && arr[trav] == 1)
        {
            trav++;
            c++;
        }
        c += k;
     
        // Compute the maximum length
        if (c > maximum)
            maximum = c;
    }
     
    // Return the length
    return maximum;
}
 
// Driver code
public static void main(String args[])
{
    int k = 3;
     
    // Array initialization
    int arr[] = { 0, 0, 1, 1, 0, 0, 0, 0 };
 
    // Size of array
    int n = arr.length;
    int ans = findmax(arr, n, k);
     
    System.out.println(ans);
}
}
 
// This code is contributed by Stream_Cipher




# Python3 program for the above approach
 
# Function to find the maximum number of
# consecutive 1's after flipping all
# zero in a K length subarray
def findmax(arr, n, k):
     
    # Initialize variable
    trav, i = 0, 0
    c = 0
    maximum = 0
 
    # Iterate until n-k+1 as we
    # have to go till i+k
    while i < n - k + 1:
        trav = i - 1
        c = 0
 
        # Iterate in the array in left direction
        # till you get 1 else break
        while trav >= 0 and arr[trav] == 1:
            trav -= 1
            c += 1
        trav = i + k
 
        # Iterate in the array in right direction
        # till you get 1 else break
        while (trav < n and arr[trav] == 1):
            trav += 1
            c += 1
 
        c += k
 
        # Compute the maximum length
        if (c > maximum):
            maximum = c
        i += 1
 
    # Return the length
    return maximum
 
# Driver code
if __name__ == '__main__':
    k = 3
     
    # Array initialization
    arr = [0, 0, 1, 1, 0, 0, 0, 0]
 
    # Size of array
    n = len(arr)
    ans = findmax(arr, n, k)
    print(ans)
 
# This code is contributed by Mohit Kumar




// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the maximum number of
// consecutive 1's after flipping all
// zero in a K length subarray
static int findmax(int []arr, int n, int k)
{
     
    // Initialize variable
    int trav, i;
    int c = 0, maximum = 0;
     
    // Iterate until n-k+1 as we
    // have to go till i+k
    for(i = 0; i < n - k + 1; i++)
    {
        trav = i - 1;
        c = 0;
     
        // Iterate in the array in left direction
        // till you get 1 else break
        while (trav >= 0 && arr[trav] == 1)
        {
            trav--;
            c++;
        }
        trav = i + k;
     
        // Iterate in the array in right direction
        // till you get 1 else break
        while (trav < n && arr[trav] == 1)
        {
            trav++;
            c++;
        }
        c += k;
     
        // Compute the maximum length
        if (c > maximum)
            maximum = c;
    }
     
    // Return the length
    return maximum;
}
 
// Driver code
public static void Main()
{
    int k = 3;
     
    // Array initialization
    int []arr = { 0, 0, 1, 1, 0, 0, 0, 0 };
 
    // Size of array
    int n = arr.Length;
    int ans = findmax(arr, n, k);
     
    Console.WriteLine(ans);
}
}
 
// This code is contributed by Stream_Cipher




<script>
// Javascript program for the above approach
 
// Function to find the maximum number of
// consecutive 1's after flipping all
// zero in a K length subarray
function findmax(arr, n, k) {
    // Initialize variable
    let trav, i;
    let c = 0, maximum = 0;
 
    // Iterate until n-k+1 as we
    // have to go till i+k
    for (i = 0; i < n - k + 1; i++) {
        trav = i - 1;
        c = 0;
 
        /*Iterate in the array in left direction
        till you get 1 else break*/
        while (trav >= 0 && arr[trav] == 1) {
            trav--;
            c++;
        }
        trav = i + k;
 
        /*Iterate in the array in right direction
        till you get 1 else break*/
        while (trav < n && arr[trav] == 1) {
            trav++;
            c++;
        }
        c += k;
 
        // Compute the maximum length
        if (c > maximum)
            maximum = c;
    }
 
    // Return the length
    return maximum;
}
 
// Driver code
 
let k = 3;
// Array initialization
let arr = [0, 0, 1, 1, 0, 0, 0, 0];
 
// Size of array
let n = arr.length;
let ans = findmax(arr, n, k);
document.write(ans)
 
// This code is contributed by _saurabh_jaiswal.
</script>

Output
5

Time complexity: O(N2)

Auxiliary Space: O(1)


Article Tags :