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

## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

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 ` `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); ` `    ``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

 `= 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``); ` `echo` `longestSubarry(``\$arr``, ``\$n``); ` ` `  `// This code is contributed by nitin mittal. ` `?> `

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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Improved By : nitin mittal

Article Tags :
Practice Tags :

6

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