Longest Subarray of non-negative Integers
Given an array, return the length of the longest subarray of non-negative integers
Examples :
Input : {2, 3, 4, -1, -2, 1, 5, 6, 3}
Output : 4
Explanation: The subarray [ 1, 5, 6, 3] has length 4 and contains no negative integers
Input : {1, 0, 0, 1, -1, -1, 0, 0, 1, 0}
Output : 4
Explanation: Subarrays [1, 0, 0, 1] and [0, 0, 1, 0] have equal lengths but sum of first one is greater so that will be the output.
Naive Approach: The idea is to use two nested loops, one for choosing starting index of a valid subarray (where all elements are positive) and the other for finding the ending index of a valid subarray. Keep updating the result if we find any longer valid subarray.
Below is the implementation of the above approach
C++
#include <iostream>
#include <string>
#include <vector>
class GFG
{
public :
static int longestSubarray(std::vector< int > &A)
{
int n = A.size();
int result = 0;
for ( int i = 0; i < n; i++)
{
if (A[i] < 0)
{
continue ;
}
int j = i;
for (; j < n; j++)
{
if (A[j] < 0)
{
--j;
break ;
}
}
if (j - i + 1 > result)
{
result = j - i + 1;
}
}
return result;
}
static void main(std::vector<std::string> &args)
{
std::vector< int > arr{1, 0, 4, 0, 1, -1, -1, 0, 0, 1, 0};
int n = arr.size();
std::cout << GFG::longestSubarray(arr) << std::endl;
}
};
int main( int argc, char **argv){
std::vector<std::string> parameter(argv + 1, argv + argc);
GFG::main(parameter);
return 0;
};
|
Java
import java.io.*;
class GFG {
public static int longestSubarray( int [] A)
{
int n = A.length;
int result = 0 ;
for ( int i = 0 ; i < n; i++) {
if (A[i] < 0 )
continue ;
int j = i;
for (; j < n; j++) {
if (A[j] < 0 ) {
--j;
break ;
}
}
if (j - i + 1 > result) {
result = j - i + 1 ;
}
}
return result;
}
public static void main(String[] args)
{
int arr[] = { 1 , 0 , 4 , 0 , 1 , - 1 , - 1 , 0 , 0 , 1 , 0 };
int n = arr.length;
System.out.println(longestSubarray(arr));
}
}
|
Python3
def longestSubarray(A):
n = len (A)
result = 0
for i in range (n):
if A[i] < 0 :
continue
for j in range (i, n):
if A[j] < 0 :
j - = 1
break
if j - i + 1 > result:
result = j - i + 1
return result
arr = [ 1 , 0 , 4 , 0 , 1 , - 1 , - 1 , 0 , 0 , 1 , 0 ]
n = len (arr)
print (longestSubarray(arr))
|
C#
using System;
public class GFG {
public static int longestSubarray( int [] A)
{
int n = A.Length;
int result = 0;
for ( int i = 0; i < n; i++) {
if (A[i] < 0)
continue ;
int j = i;
for (; j < n; j++) {
if (A[j] < 0) {
--j;
break ;
}
}
if (j - i + 1 > result) {
result = j - i + 1;
}
}
return result;
}
static public void Main()
{
int [] arr = { 1, 0, 4, 0, 1, -1, -1, 0, 0, 1, 0 };
int n = arr.Length;
Console.WriteLine(longestSubarray(arr));
}
}
|
Javascript
<script>
function longestSubarray(A,n)
{
let result = 0;
for (let i = 0; i < n; i++)
{
if (A[i] < 0)
{
continue ;
}
let j = i;
for (; j < n; j++)
{
if (A[j] < 0)
{
--j;
break ;
}
}
if (j - i + 1 > result)
{
result = j - i + 1;
}
}
return result;
}
let arr=[1, 0, 4, 0, 1, -1, -1, 0, 0, 1, 0];
let n = 11;
document.write(longestSubarray(arr,n));
</script>
|
Time Complexity: O(N2), Where N is the length of the given array
Auxiliary Space: O(1)
Efficient Approach: We follow a simple two-pointer window approach. Initially start index points to the starting of curr subarray i.e. a non-negative integer and traverses the array. Whenever a negative element occurs we compare it with the length of the max subarray so far and update the start and size if curr length is greater. finally, we return the subarray starting at the start of length size with the first element as the size of the subarray.
Implementation:
C++
#include<iostream>
using namespace std;
int longestSubarry( int *arr, int n)
{
int res = 0;
for ( int i = 0; i < n; i++)
{
int curr_count = 0;
while (i < n && arr[i] >= 0)
{
curr_count++;
i++;
}
res = max(res, curr_count);
}
return res;
}
int main()
{
int arr[] = {1, 0, 4, 0, 1, -1, -1,
0, 0, 1, 0};
int n = sizeof (arr) / sizeof (arr[0]);
cout << longestSubarry(arr, n);
return 0;
}
|
Java
class GFG
{
static int longestSubarry( int arr[], int n)
{
int res = 0 ;
for ( int i = 0 ; i < n; i++)
{
int curr_count = 0 ;
while (i < n && arr[i] >= 0 )
{
curr_count++;
i++;
}
res = Math.max(res, curr_count);
}
return res;
}
public static void main(String[] args)
{
int arr[] = { 1 , 0 , 4 , 0 , 1 , - 1 ,
- 1 , 0 , 0 , 1 , 0 };
int n = arr.length;
System.out.println(longestSubarry(arr, n));
}
}
|
Python3
def longestSubarry(arr,n):
res = 0
for i in range (n):
curr_count = 0
while (i < n and arr[i] > = 0 ):
curr_count + = 1
i + = 1
res = max (res, curr_count)
return res
arr = [ 1 , 0 , 4 , 0 , 1 , - 1 , - 1 , 0 , 0 , 1 , 0 ]
n = len (arr)
print (longestSubarry(arr, n))
|
C#
using System;
class GFG
{
static int longestSubarry( int []arr, int n)
{
int res = 0;
for ( int i = 0; i < n; i++)
{
int curr_count = 0;
while (i < n && arr[i] >= 0)
{
curr_count++;
i++;
}
res = Math.Max(res, curr_count);
}
return res;
}
public static void Main()
{
int []arr = {1, 0, 4, 0, 1, -1,
-1, 0, 0, 1, 0};
int n = arr.Length;
Console.Write(longestSubarry(arr, n));
}
}
|
PHP
<?php
function longestSubarry( $arr , $n )
{
$res = 0;
for ( $i = 0; $i < $n ; $i ++)
{
$curr_count = 0;
while ( $i < $n && $arr [ $i ] >= 0)
{
$curr_count ++;
$i ++;
}
$res = max( $res , $curr_count );
}
return $res ;
}
$arr = array (1, 0, 4, 0, 1, -1,
-1, 0, 0, 1, 0);
$n = sizeof( $arr ) / sizeof( $arr [0]);
echo longestSubarry( $arr , $n );
?>
|
Javascript
<script>
function longestSubarry(arr, n)
{
let res = 0;
for (let i = 0; i < n; i++)
{
let curr_count = 0;
while (i < n && arr[i] >= 0)
{
curr_count++;
i++;
}
res = Math.max(res, curr_count);
}
return res;
}
var arr = [1, 0, 4, 0, 1, -1, -1,
0, 0, 1, 0];
let n = arr.length;
document.write(longestSubarry(arr, n));
</script>
|
Time Complexity: O(N), Where N is the length of the given array
Auxiliary Space: O(1)
Exercise :
Modify the above solution to print the result subarray. Also, in case two subarrays have the same count, print the subarray with a larger sum.
Last Updated :
16 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...