Longest Subarray of non-negative Integers

Given an array, return length of the longest subarray of non- negative integers

Examples :

Input : {2, 3, 4, -1, -2, 1, 5, 6, 3}
Output : 4

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

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.

We follow a simple two pointer window approach. Initially si points to the starting of curr subarray i.e. a non negative integer.ei is used to traverse the array. Whenever a negative element occurs we compare with the length of max subarray so far and update start and size if curr length is greater. finally we return the subarray starting at start of length size with first element as size of the subarray.

C++



filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find length of the longest
// subarray with non-negative numbers.
#include<iostream>
using namespace std;
  
// Function that returns the longest 
// subarray of non-negative integers 
int longestSubarry(int *arr, int n)
{
    // Initialize result
    int res = 0; 
      
    // Traverse array
    for (int i = 0; i < n; i++)
    {
        // Count of current 
        // non-negative integers
        int curr_count = 0;
          
        while (i < n && arr[i] >= 0)
        {
            curr_count++;
            i++;
        }
      
        // Update result if required.
        res = max(res, curr_count);
    }
      
    return res;
}
  
// Driver code
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find length of the longest
// subarray with non-negative numbers.
  
class GFG 
{
  
    // Function that returns the longest
    // subarray of non-negative integers 
    static int longestSubarry(int arr[], int n)
    {
        // Initialize result
        int res = 0
          
        // Traverse array
        for (int i = 0; i < n; i++)
        {
            // Count of current non-
            // negative integers
            int curr_count = 0;
            while (i < n && arr[i] >= 0)
            {
                curr_count++;
                i++;
            }
          
            // Update result if required.
            res = Math.max(res, curr_count);
        }
          
        return res;
    }
      
    // Driver code
    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));
    }
}
  
// This code is contributed by prerna saini.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to find
# length of the longest
# subarray with
# non-negative numbers.
  
# Function that returns
# the longest subarray
# of non-negative integers 
def longestSubarry(arr,n):
  
    # Initialize result
    res = 0 
  
    # Traverse array
    for i in range(n):
      
        # Count of current 
        # non-negative integers
        curr_count = 0
        while (i < n and arr[i] >= 0):
      
            curr_count+=1
            i+=1
      
        # Update result if required.
        res = max(res, curr_count)
      
    return res
  
  
# Driver code
  
arr= [1, 0, 4, 0, 1, -1, -1, 0, 0, 1, 0]
n = len(arr)
print(longestSubarry(arr, n))
  
# This code is contributed
# by Anant Agarwal.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find length of the longest
// subarray with non-negative numbers.
using System;
  
class GFG 
{
  
    // Function that returns the longest
    // subarray of non-negative integers
    static int longestSubarry(int []arr, int n)
    {
        // Initialize result
        int res = 0; 
          
        // Traverse array
        for (int i = 0; i < n; i++)
        {
            // Count of current non-
            // negative integers
            int curr_count = 0;
            while (i < n && arr[i] >= 0)
            {
                curr_count++;
                i++;
            }
          
            // Update result if required.
            res = Math.Max(res, curr_count);
        }
          
        return res;
    }
      
    // Driver code
    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));
    }
}
  
// This code is contributed by nitin mittal

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find length
// of the longest subarray with
// non-negative numbers.
  
// Function that returns the longest 
// subarray of non-negative integers 
function longestSubarry($arr, $n)
{
    // Initialize result
    $res = 0; 
      
    // Traverse array
    for ($i = 0; $i < $n; $i++)
    {
        // Count of current 
        // non-negative integers
        $curr_count = 0;
          
        while ($i < $n && $arr[$i] >= 0)
        {
            $curr_count++;
            $i++;
        }
      
        // Update result if required.
        $res = max($res, $curr_count);
    }
      
    return $res;
}
  
// Driver code
$arr = array(1, 0, 4, 0, 1, -1,
            -1, 0, 0, 1, 0);
$n = sizeof($arr) / sizeof($arr[0]);
echo longestSubarry($arr, $n);
  
// This code is contributed by nitin mittal.
?>

chevron_right



Output :

5

Time Complexity : O(n)

Exercise :
Modify above solution to print the result subarray. Also, in case two subarrays have same count, print the subarray with larger sum.

This article is contributed by Aditi Sharma. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : nitin mittal



Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.