Length of longest subarray | Practice | GeeksforGeeks

Given an array A[] of size N, return length of the longest subarray of non- negative integers.
Note: Subarray here means a continuous part of the array.
 
Example 1:
Input : 
N = 9
A[] = {2, 3, 4, -1, -2, 1, 5, 6, 3}
Output : 
4
Explanation :
The subarray [ 1, 5, 6, 3] has longest length 4 and
contains no negative integers
Input : 
N = 10
A[] = {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.



Your Task:  
You don't need to read input or print anything. Your task is to complete the function longestSubarray() which takes the array A[] and its size N as inputs and returns the length of the longest subarray of non-negative integers.
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= N <= 105
-105 <= A[i] <= 105
0
varun1404be202 months ago
int max=0;   int i=0;   int j=0;   while(i<N && j<N){       while(A[j]>=0 && j<N){           // cout<<j<<" "<<i<<" ";                      j++;       }       // cout<<endl;       if(max<j-i){           // cout<<i<<" "<<j<<endl;           max=j-i;       }       i=j;       i++;       j++;          }
0
dkudalkarsr2j2 months ago
Python3 solution by sliding window:
 
def longestSubarry( A, N):
    l=0
    m=0
    curr=0
    for r in range(N):
        if A[r]>=0:
            curr+=1
            m=max(m,curr)
        else:
            l=r
            l+=1
            curr=0
    m=max(m,curr)
    return m
0
yanoop2383 months ago
      int i=0;   int ans=0;   int count=0;   while(i<N){       if(A[i]>0){           count++;           ans=max(ans,count);           i++;           continue;       }       if(A[i]<0){           i++;
       }       count=0;          }   return ans; 
0
amiransarimy3 months ago
Simple Python Solutions
Total Time Taken:
0.6/6.0
 
def longestSubarry( A, N):
   curr_length, max_length = 0,0
   
   for idx in range(N):
       
       if A[idx] >=0:
           curr_length += 1
           max_length = max(max_length,curr_length)
        
       else:
            curr_length = 0 #reset value
            
   return max_length
  
 
+1
imranwahid4 months ago
Easy C++ solution using two pointers
0
badgujarsachin834 months ago
long long int longestSubarry(long long int A[], long long int N) {
        long long int ans=-1000000;    
        long long int c=0;
        for(int j=0;j<N;j++){
            if(A[j]>=0){
                   c++;
              }else{
                    
                    ans=max(ans,c);
                    c=0;
                }
                
         }
        ans=max(ans,c);
        return ans;
}
-2
akshatmathur234 months ago
JAVA SOLUTION - 
 public long longestSubarry(long A[], int N)
    {
        int maxLength = Integer.MIN_VALUE;
        int length = 0;
        for(int i = 0; i < N; i++)
        {
            if(A[i] >= 0)
                length++;
            else 
            {
                if(length > maxLength)
                    maxLength = length;
                length = 0;    
            }
        }
        return length > maxLength ? maxLength = length : maxLength;
    }
0
akshatmathur23
This comment was deleted.
0
pketul22125 months ago
class Compute {
    
    public long longestSubarry(long A[], int N)
    {
        
        long len = 0, max = 0;
        
        for(int i = 0; i < N; i++) {
            if(A[i] > 0)
                len++;
            
            else if(len > max) {
                max = len;
                len = 0;
            }
            
            if(A[i] < 0)
                len = 0;
        }
        
        if(len > max)
            max = len;
        
        return max;
    }
}
-2
ankitsinha1112007 months ago
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.