Related Articles
Longest subarray of non-empty cells after removal of at most a single empty cell
• Difficulty Level : Medium
• Last Updated : 07 Apr, 2021

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:

• Initialize two arrays l[] and r[] which stores the length of longest consecutive 1s in the array arr[] from left and right side of the array respectively.
• Iterate over the input array over indices (0, N) and increase count by 1 for every arr[i] = 1. Otherwise, store the value of count till the (i – 1)th index in l[i] reset count to zero.

• Similarly, repeat the above steps by traversing over indices [N – 1, 0] store the count from right in r[].
• For every ith index index which contains 0, calculate the length of non-empty subarray possible by removal of that 0, which is equal to l[i] + r[i].
• Compute the maximum of all such lengths and print the result.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``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;``}`

## Java

 `// 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`

## Python3

 `# 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`

## C#

 `// 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`

## Javascript

 ``
Output:
`4`

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live and Geeks Classes Live USA

My Personal Notes arrow_drop_up