Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Longest Subarray of non-negative Integers

  • Difficulty Level : Easy
  • Last Updated : 24 Jun, 2021

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.

 

Hey! Looking for some great resources suitable for young ones? You've come to the right place. Check out our self-paced courses designed for students of grades I-XII

Start with topics like Python, HTML, ML, and learn to make some games and apps all with the help of our expertly designed content! So students worry no more, because GeeksforGeeks School is now here!

 



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++




// 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;
}

Java




// 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.

Python3




# 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.

C#




// 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

PHP




<?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.
?>

Javascript




<script>
 // JavaScript program for the above approach
 
 
// Function that returns the longest
// subarray of non-negative integers
function longestSubarry(arr, n)
{
    // Initialize result
    let res = 0;
     
    // Traverse array
    for (let i = 0; i < n; i++)
    {
        // Count of current
        // non-negative integers
        let 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
 
    var arr = [1, 0, 4, 0, 1, -1, -1,
                           0, 0, 1, 0];
    let n = arr.length;
    document.write(longestSubarry(arr, n));
    
 
 
// This code is contributed by Potta Lokesh
    </script>

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 write.geeksforgeeks.org or mail your article to review-team@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
Recommended Articles
Page :

Start Your Coding Journey Now!