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++
// 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
JavaScript
<script>
// javascript program for the above approach
// Function to find the maximum length
// of a subarray of 1s after removing
// at most one 0
function longestSubarray(a , n)
{
// Stores the count of consecutive
// 1's from left
var l = Array(n).fill(0);
// Stores the count of consecutive
// 1's from right
var r = Array(n).fill(0);
// Traverse left to right
for (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 (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
var ans = -1;
for (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
var arr = [ 0, 1, 1, 1, 0, 1, 0, 1, 1 ];
var n = arr.length;
document.write(longestSubarray(arr, n));
// This code is contributed by Rajput-Ji
</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
Similar Reads
Longest subarray such that adjacent elements have at least one common digit | Set 1 Given an array of N integers, write a program that prints the length of the longest subarray such that adjacent elements of the subarray have at least one digit in common. Examples: Input : 12 23 45 43 36 97 Output : 3 Explanation: The subarray is 45 43 36 which has 4 common in 45, 43 and 3 common i
11 min read
Length of longest subset consisting of A 0s and B 1s from an array of strings | Set 2 Given an array arr[] consisting of N binary strings, and two integers A and B, the task is to find the length of the longest subset consisting of at most A 0s and B 1s. Examples: Input: arr[] = {â1â, â0â, â0001â, â10â, â111001â}, A = 5, B = 3Output: 4Explanation: One possible way is to select the su
13 min read
Length of longest subset consisting of A 0s and B 1s from an array of strings Given an array arr[] consisting of binary strings, and two integers a and b, the task is to find the length of the longest subset consisting of at most a 0s and b 1s.Examples:Input: arr[] = ["1" ,"0" ,"0001" ,"10" ,"111001"], a = 5, b = 3Output: 4Explanation: One possible way is to select the subset
15+ min read
Count of contiguous subarrays possible for every index by including the element at that index Given a number N which represents the size of the array A[], the task is to find the number of contiguous subarrays that can be formed for every index of the array by including the element at that index in the original array. Examples: Input: N = 4 Output: {4, 6, 6, 4} Explanation: Since the size of
8 min read
Total number of unit cells covered by all given Rectangles Given a matrix A[][] consisting of coordinates of N rectangles such that {A[i][0], A[i][1]} representing bottom left coordinate of rectangle and {A[i][2], A[i][3]} representing top right coordinate of rectangle, the task is to find the total number of cells covered by all rectangles. Examples: Input
8 min read