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++
// 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. ?> |
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.
Recommended Posts:
- Find subarray with given sum | Set 1 (Nonnegative Numbers)
- Longest increasing subarray
- Longest subarray having maximum sum
- Longest subarray such that the difference of max and min is at-most one
- Longest subarray with sum divisible by k
- Longest Subarray having sum of elements atmost 'k'
- Longest subarray in which all elements are greater than K
- Longest Subarray with Sum greater than Equal to Zero
- Longest subarray not having more than K distinct elements
- Longest Subarray having strictly positive XOR
- Length of the longest Subarray with only Even Elements
- Length of the longest alternating subarray
- Longest sequence of positive integers in an array
- Longest subarray having difference in the count of 1's and 0's equal to k
- Longest Prime Subarray after removing one element
Improved By : nitin mittal


