Longest increasing subarray | Practice | GeeksforGeeks

Given an array containing n numbers. The problem is to find the length of the longest contiguous subarray such that every element in the subarray is strictly greater than its previous element in the same subarray.
 
Example 1:
Input:
n = 9
a[] = {5, 6, 3, 5, 7, 8, 9, 1, 2}
Output:
5
 
Example 2:
Input:
n = 10
a[] = {12, 13, 1, 5, 4, 7, 8, 10, 10, 11}
Output:
4
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function lenOfLongIncSubArr() which takes the array a[] and its size n as inputs and returns the length of the longest increasing subarray.
 
Expected Time Complexity: O(n)
Expected Auxiliary Space: O(1)
 
Constraints: 
1<=n<=105
1<=a[i]<=105
 
0
mayank20214 weeks ago
C++ : 0.12/1.25long int lenOfLongIncSubArr(long int arr[], long int n) {             int finalcount=0, tempcount=1;            for(int i=0; i<n-1 ; i++)       {           if(arr[i]>= arr[i+1])             {             finalcount=max(finalcount, tempcount);             tempcount=1;             }           else             tempcount++;                  }       finalcount=max(finalcount, tempcount);              return finalcount;                    }
+1
hasnainraza1998hr2 months ago
C++, 0.1
long int lenOfLongIncSubArr(long int arr[], long int n) {       long int max=1,temp=1;       for(int i=0;i<n-1;i++){           if(arr[i]<arr[i+1])               temp++;           else if(arr[i]>arr[i+1])               temp=1;           if(temp>max)               max=temp;       }       return max;   }
0
jhasuraj73812 months ago
//java code
 int max=1,count=1;       for(int i=0;i<n-1;i++)       {                      if(arr[i+1]>arr[i])               count++;           if(arr[i+1]<arr[i])              count=1;           if(count>max)               max=count;                             }       return max;
0
vaibhavikhachane304 months ago
//Java Solution
class Solution {
    
    public long lenOfLongIncSubArr(long arr[], long n)
    {
        
        long len = 1;
        long maxLen = 1;
       
        
        for (int i = 1; i < n; i++){
            if(arr[i-1] < arr[i]){
                len ++;
            }
            else{
                len = 1;
            }
          maxLen = Math.max(len, maxLen);
        }
        return maxLen;
    }
}
0
codersheaven694 months ago
Python solution
 
 
 max_count = 0       count = 0       # final = []       for i in range(n-1):           if int(a[i+1])>int(a[i]):               count +=1               # final.append(count)           else:               count = 0           max_count = max(count,max_count)                             return max_count +1
+1
badgujarsachin837 months ago
 long int lenOfLongIncSubArr(long int arr[], long int n) {
    long  int c=1;
    long int max=1;
        for(int i=1;i<n;i++){
            if(arr[i]>arr[i-1]){
                c++;
                if(c>max){
                    max=c;
                }
            }else{
               
                c=1;
            }
            
        }
        return max;
    }
+2
jasmeenkaursyali7 months ago
long int lenOfLongIncSubArr(long int arr[], long int n) {       if(n==1) return 1;              long int c=1,maxlen=0;       for(int i=1;i<n;i++){           if(arr[i-1]<arr[i])           c++;           else           c=1;           maxlen = max(maxlen,c);       }       return maxlen;   }
0
samughodake18087 months ago
long int lenOfLongIncSubArr(long int a[], long int n) {       long int max_count=0;       long int count=0;       for(int i=0;i<n-1;i++){           if(a[i]<a[i+1]){               count++;           }           else{               count=0;           }           max_count=max(count,max_count);       }       return max_count+1;   }
0
This comment was deleted.
0
Manvendra Singh Chhajerh11 months ago
Manvendra Singh Chhajerh
if(n<1)
            return 0;
        long int clen = 1, max_len =1;
        for(long int i=1;i<n;i++) {="" if(arr[i]=""> arr[i-1])
                clen++;
            else
                clen = 1;
            if (clen > max_len)
                max_len = clen;
        }
        return max_len;
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.