Length of longest subarray consisting only of 1s
Last Updated :
28 Jun, 2022
Given an array arr[] of size N, consisting of binary values, the task is to find the longest non-empty subarray consisting only of 1s after removal of a single array element.
Examples:
Input: arr[] = {1, 1, 1}
Output: 2
Input: arr[] = {0, 0, 0}
Output: 0
Approach: Follow the steps below to solve the problem:
- Initialize three variables, newLen = 0, prevLen = 0, maxLen = 0.
- Traverse the array arr[] by appending zero at the beginning:
- If arr[i] = 1: Increment both newLen & prevLen by 1.
- Otherwise:
- Assign the maximum value to the variable maxLen.
- Set prevLen = newLen and newLen = 0.
- Print maxLen if maxLen < len(arr).
- Otherwise, print maxLen – 1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int longestSubarrayUtil(vector< int > arr, int n)
{
int neww = 0, old = 0, m = 0;
for ( int x = 0; x <= n; x++)
{
if (arr[x] == 1)
{
neww += 1;
old += 1;
}
else
{
m = max(m, old);
old = neww;
neww = 0;
}
}
if (m < n)
{
return m;
}
else return m - 1;
}
void longestSubarray(vector< int > arr, int n)
{
int len = longestSubarrayUtil(arr, n);
cout << len;
}
int main()
{
vector< int > arr = {1, 1, 1};
int n = arr.size();
for ( int i = n; i >= 0; i--)
{
arr[i] = arr[i - 1];
}
arr[0] = 0;
longestSubarray(arr, n);
}
|
Java
import java.util.*;
class GFG
{
static int longestSubarrayUtil( int [] arr, int n)
{
int neww = 0 , old = 0 , m = 0 ;
for ( int x = 0 ; x <n; x++)
{
if (arr[x] == 1 )
{
neww += 1 ;
old += 1 ;
}
else
{
m = Math.max(m, old);
old = neww;
neww = 0 ;
}
}
m = Math.max(m, old);
if (m<n)
return m;
else
return m- 1 ;
}
static void longestSubarray( int []arr, int n)
{
int len = longestSubarrayUtil(arr, n);
System.out.print(len);
}
public static void main(String[] args)
{
int arr[] = { 1 , 1 , 1 };
int n = arr.length;
longestSubarray(arr, n);
}
}
|
C#
using System;
public class GFG {
static void longestSubarray( int [] arr, int n)
{
int len = longestSubarrayUtil(arr, n);
Console.WriteLine(len);
}
static int longestSubarrayUtil( int [] arr, int n)
{
int neww = 0, old = 0, m = 0;
for ( int x = 0; x < n; x++) {
if (arr[x] == 1) {
neww += 1;
old += 1;
}
else {
m = Math.Max(m, old);
old = neww;
neww = 0;
}
}
m = Math.Max(m, old);
if (m < n)
return m;
else
return m - 1;
}
static public void Main()
{
int [] arr = { 1, 1, 1 };
int n = arr.Length;
longestSubarray(arr, n);
}
}
|
Python3
def longestSubarrayUtil(arr):
new, old, m = 0 , 0 , 0
for x in arr + [ 0 ]:
if x = = 1 :
new + = 1
old + = 1
else :
m = max (m, old)
old, new = new, 0
return m if m < len (arr) else m - 1
def longestSubarray(arr):
len = longestSubarrayUtil(arr)
print ( len )
arr = [ 1 , 1 , 1 ]
longestSubarray(arr)
|
Javascript
<script>
function longestSubarrayUtil(arr, n)
{
var neww = 0, old = 0, m = 0;
for ( var x = 0; x <= n; x++)
{
if (arr[x] == 1)
{
neww += 1;
old += 1;
}
else
{
m = Math.max(m, old);
old = neww;
neww = 0;
}
}
if (m < n)
{
return m;
}
else
{
return m - 1;
}
}
function longestSubarray(arr, n)
{
var len = longestSubarrayUtil(arr, n);
document.write( len);
}
var arr = [1, 1, 1];
var n = arr.length;
for ( var i = n-1; i >= 0; i--)
{
arr[i] = arr[i - 1];
}
arr[0] = 0;
longestSubarray(arr, n);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...