Longest subarray of non-empty cells after removal of at most a single empty cell
Last Updated :
11 Jul, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int longestSubarray( int a[], int n)
{
int l[n];
int r[n];
for ( int i = 0, count = 0;
i < n; i++) {
if (a[i] == 1)
count++;
else {
l[i] = count;
count = 0;
}
}
for ( int i = n - 1, count = 0;
i >= 0; i--) {
if (a[i] == 1)
count++;
else {
r[i] = count;
count = 0;
}
}
int ans = -1;
for ( int i = 0; i < n; ++i) {
if (a[i] == 0)
ans = max(ans, l[i] + r[i]);
}
return ans < 0 ? n : ans;
}
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
class GFG{
public static int longestSubarray( int [] a,
int n)
{
int [] l = new int [n];
int [] r = new int [n];
for ( int i = 0 , count = 0 ;
i < n; i++)
{
if (a[i] == 1 )
count++;
else
{
l[i] = count;
count = 0 ;
}
}
for ( int i = n - 1 , count = 0 ;
i >= 0 ; i--)
{
if (a[i] == 1 )
count++;
else
{
r[i] = count;
count = 0 ;
}
}
int ans = - 1 ;
for ( int i = 0 ; i < n; ++i)
{
if (a[i] == 0 )
ans = Math.max(ans, l[i] + r[i]);
}
return ans < 0 ? n : ans;
}
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));
}
}
|
Python3
def longestSubarray(a, n):
l = [ 0 ] * (n)
r = [ 0 ] * (n)
count = 0
for i in range (n):
if (a[i] = = 1 ):
count + = 1
else :
l[i] = count
count = 0
count = 0
for i in range (n - 1 , - 1 , - 1 ):
if (a[i] = = 1 ):
count + = 1
else :
r[i] = count
count = 0
ans = - 1
for i in range (n):
if (a[i] = = 0 ):
ans = max (ans, l[i] + r[i])
return ans < 0 and n or ans
arr = [ 0 , 1 , 1 , 1 , 0 , 1 , 0 , 1 , 1 ]
n = len (arr)
print (longestSubarray(arr, n))
|
C#
using System;
class GFG{
public static int longestSubarray( int [] a,
int n)
{
int [] l = new int [n];
int [] r = new int [n];
for ( int i = 0, count = 0; i < n; i++)
{
if (a[i] == 1)
count++;
else
{
l[i] = count;
count = 0;
}
}
for ( int i = n - 1, count = 0;
i >= 0; i--)
{
if (a[i] == 1)
count++;
else
{
r[i] = count;
count = 0;
}
}
int ans = -1;
for ( int i = 0; i < n; ++i)
{
if (a[i] == 0)
ans = Math.Max(ans, l[i] + r[i]);
}
return ans < 0 ? n : ans;
}
public static void Main()
{
int [] arr = { 0, 1, 1, 1, 0,
1, 0, 1, 1 };
int n = arr.Length;
Console.Write(longestSubarray(arr, n));
}
}
|
Javascript
<script>
function longestSubarray(a , n)
{
var l = Array(n).fill(0);
var r = Array(n).fill(0);
for (i = 0, count = 0; i < n; i++)
{
if (a[i] == 1)
count++;
else {
l[i] = count;
count = 0;
}
}
for (i = n - 1, count = 0; i >= 0; i--) {
if (a[i] == 1)
count++;
else {
r[i] = count;
count = 0;
}
}
var ans = -1;
for (i = 0; i < n; ++i) {
if (a[i] == 0)
ans = Math.max(ans, l[i] + r[i]);
}
return ans < 0 ? n : ans;
}
var arr = [ 0, 1, 1, 1, 0, 1, 0, 1, 1 ];
var n = arr.length;
document.write(longestSubarray(arr, n));
</script>
|
Time Complexity: O(N) where n is number of elements in given array. As, we are using a loop to traverse N times so it will cost us O(N) time
Auxiliary Space: O(N), as we are using extra space.
Related Topic: Subarrays, Subsequences, and Subsets in Array
Share your thoughts in the comments
Please Login to comment...