Longest subarray of non-empty cells after removal of at most a single empty cell

Given a binary array arr[], the task is to find the longest subarray of non-empty cells after the removal of at most 1 empty cell.
 

The array indices filled with 0 are known as empty cell whereas the indices filled with 1 are known as non-empty cells.

Examples: 
 

Input: arr[] = {1, 1, 0, 1} 
Output:
Explanation: 
Removal of 0 modifies the array to {1, 1, 1}, thus maximizing the length of the subarray to 3.
Input: arr[] = {1, 1, 1, 1, 1} 
Output:
 

 



Approach: 
The idea is to store the frequencies of 1 in the prefixes and suffixes of every index to calculate longest consecutive sequences of 1’s on both the directions from a particular index. Follow the steps below to solve the problem: 
 

Below is the implementation of the above approach:
 

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach 
#include <bits/stdc++.h> 
using namespace std; 
  
// Function to find the maximum length 
// of a subarray of 1s after removing 
// at most one 0 
int longestSubarray(int a[], int n) 
    // Stores the count of consecutive 
    // 1's from left 
    int l[n]; 
  
    // Stores the count of consecutive 
    // 1's from right 
    int r[n]; 
  
    // Traverse left to right 
    for (int i = 0, count = 0; 
        i < n; i++) { 
  
        // If cell is non-empty 
        if (a[i] == 1) 
  
            // Increase count 
            count++; 
  
        // If cell is empty 
        else
  
            // Store the count of 
            // consecutive 1's 
            // till (i - 1)-th index 
            l[i] = count; 
            count = 0; 
        
    
  
    // Traverse from right to left 
    for (int i = n - 1, count = 0; 
        i >= 0; i--) { 
  
        if (a[i] == 1) 
            count++; 
  
        else
  
            // Store the count of 
            // consecutive 1s 
            // till (i + 1)-th index 
            r[i] = count; 
            count = 0; 
        
    
  
    // Stores the length of 
    // longest subarray 
    int ans = -1; 
    for (int i = 0; i < n; ++i) { 
  
        if (a[i] == 0) 
  
            // Store the maximum 
            ans = max(ans, l[i] + r[i]); 
    
  
    // If array a contains only 1s 
    // return n else return ans 
    return ans < 0 ? n : ans; 
  
// Driver Code 
int main() 
    int arr[] = { 0, 1, 1, 1, 0, 1, 
                0, 1, 1 }; 
  
    int n = sizeof(arr) / sizeof(arr[0]); 
  
    cout << longestSubarray(arr, n); 
  
    return 0; 
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach 
class GFG{ 
      
// Function to find the maximum length 
// of a subarray of 1s after removing 
// at most one 0 
public static int longestSubarray(int[] a, 
                                int n) 
      
    // Stores the count of consecutive 
    // 1's from left 
    int[] l = new int[n]; 
      
    // Stores the count of consecutive 
    // 1's from right 
    int[] r = new int[n]; 
      
    // Traverse left to right 
    for(int i = 0, count = 0
            i < n; i++) 
    
          
    // If cell is non-empty 
    if (a[i] == 1
          
        // Increase count 
        count++; 
          
    // If cell is empty 
    else
    
              
        // Store the count of 
        // consecutive 1's 
        // till (i - 1)-th index 
        l[i] = count; 
        count = 0
    
    
      
    // Traverse from right to left 
    for(int i = n - 1, count = 0
            i >= 0; i--) 
    
    if (a[i] == 1
        count++; 
          
    else
    
              
        // Store the count of 
        // consecutive 1s 
        // till (i + 1)-th index 
        r[i] = count; 
        count = 0
        
    
      
    // Stores the length of 
    // longest subarray 
    int ans = -1
    for(int i = 0; i < n; ++i) 
    
    if (a[i] == 0
              
        // Store the maximum 
        ans = Math.max(ans, l[i] + r[i]); 
    
      
    // If array a contains only 1s 
    // return n else return ans 
    return ans < 0 ? n : ans; 
  
// Driver code 
public static void main(String[] args) 
    int[] arr = { 0, 1, 1, 1, 0
                1, 0, 1, 1 }; 
    int n = arr.length; 
      
    System.out.println(longestSubarray(arr, n)); 
  
// This code is contributed by divyeshrabadiya07 
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for the above approach 
  
# Function to find the maximum length 
# of a subarray of 1s after removing 
# at most one 0 
def longestSubarray(a, n):
  
    # Stores the count of consecutive 
    # 1's from left 
    l = [0] * (n) 
  
    # Stores the count of consecutive 
    # 1's from right 
    r = [0] * (n)
      
    count = 0
      
    # Traverse left to right 
    for i in range(n):
          
        # If cell is non-empty 
        if (a[i] == 1):
              
            # Increase count 
            count += 1
          
        # If cell is empty 
        else:
              
            # Store the count of 
            # consecutive 1's 
            # till (i - 1)-th index 
            l[i] = count 
            count = 0
      
    count = 0
    # Traverse from right to left
    for i in range(n - 1, -1, -1):
        if (a[i] == 1):
            count += 1
              
        else:
              
            # Store the count of 
            # consecutive 1s 
            # till (i + 1)-th index 
            r[i] = count
            count = 0
      
    # Stores the length of 
    # longest subarray 
    ans = -1
    for i in range(n):
        if (a[i] == 0):
              
            # Store the maximum 
            ans = max(ans, l[i] + r[i])
      
    # If array a contains only 1s 
    # return n else return ans 
    return ans < 0 and n or ans
  
# Driver code 
arr = [ 0, 1, 1, 1, 0, 1, 0, 1, 1
  
n = len(arr)
  
print(longestSubarray(arr, n))
  
# This code is contributed by sanjoy_62
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach 
using System;
  
class GFG{ 
      
// Function to find the maximum length 
// of a subarray of 1s after removing 
// at most one 0 
public static int longestSubarray(int[] a,
                                  int n) 
      
    // Stores the count of consecutive 
    // 1's from left
    int[] l = new int[n]; 
      
    // Stores the count of consecutive 
    // 1's from right 
    int[] r = new int[n]; 
      
    // Traverse left to right 
    for(int i = 0, count = 0; i < n; i++)
    
          
        // If cell is non-empty 
        if (a[i] == 1) 
              
            // Increase count 
            count++; 
              
        // If cell is empty 
        else
        
              
            // Store the count of 
            // consecutive 1's 
            // till (i - 1)-th index 
            l[i] = count; 
            count = 0; 
        
    
      
    // Traverse from right to left 
    for(int i = n - 1, count = 0; 
            i >= 0; i--)
    
    if (a[i] == 1)
        count++; 
          
    else
    
              
        // Store the count of 
        // consecutive 1s 
        // till (i + 1)-th index 
        r[i] = count; 
        count = 0; 
        
    
      
    // Stores the length of 
    // longest subarray 
    int ans = -1; 
    for(int i = 0; i < n; ++i)
    
        if (a[i] == 0) 
                  
            // Store the maximum 
            ans = Math.Max(ans, l[i] + r[i]); 
    
      
    // If array a contains only 1s 
    // return n else return ans 
    return ans < 0 ? n : ans; 
}
  
  
// Driver code
public static void Main()
{
    int[] arr = { 0, 1, 1, 1, 0,
                  1, 0, 1, 1 }; 
    int n = arr.Length; 
  
    Console.Write(longestSubarray(arr, n));
}
}
  
// This code is contributed by sanjoy_62
chevron_right

Output: 
4

 

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




Recommended Posts:


Amazon Alexa Student Influencer, Skilled in C, C++ , Front-end Development, HTML5, Bootstrap, Cascading Style Sheets (CSS), JavaScript and NodeJS Strong education professional with a Bachelor of Technology - BTech focused in Computer Engineering from Heritage Institute of Technology, Kolkata

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : divyeshrabadiya07, sanjoy_62

Article Tags :
Practice Tags :