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: 3
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: 5
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 <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; } |
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 |
4
Time Complexity: O(N)
Auxiliary Space: O(N)
Recommended Posts:
- Check if cells numbered 1 to K in a grid can be connected after removal of atmost one blocked cell
- Size of all connected non-empty cells of a Matrix
- Count of ways to select K consecutive empty cells from a given Matrix
- Reduce a given Binary Array to a single element by removal of Triplets
- Longest palindromic string possible after removal of a substring
- Maximize length of Subarray of 1's after removal of a pair of consecutive Array elements
- Minimum palindromic subarray removals to make array Empty
- Count of cells in a matrix whose adjacent cells's sum is prime Number
- Maximum of all distances to the nearest 1 cell from any 0 cell in a Binary matrix
- Longest subarray having sum K | Set 2
- Longest subarray such that the difference of max and min is at-most one
- Longest subarray with sum divisible by k
- Longest subarray with sum not divisible by X
- Longest subarray with odd product
- Longest subarray with only one value greater than k
- Longest Mountain Subarray
- Longest increasing subarray
- Longest subarray having maximum sum
- Longest subarray with all elements same
- Longest subarray such that difference of max and min is at-most K
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.


