Question
You are given array consisting of n integers. Your task is to find the maximum length of an increasing subarray of the given array.
.
A subarray is the sequence of consecutive elements of the array.
Subarray is called increasing if each element of this subarray strictly greater than previous.
.
Input:
The first line contains single positive integer n (1 ≤ n ≤ 105) — the number of integers.
.
The second line contains n positive integers a1, a2, …, an (1 ≤ ai ≤ 109).
.
Output:
Print the maximum length of an increasing subarray of the given array.
Sample1 Input:
5
1 7 2 11 15
Sameple1 Output:
3
Sample2 Input:
6
100 100 100 100 100 100
Sameple2 Output:
3
Stragedy
While getting the input from users, we can create a variable called “longest”, “nums” and “previous”. We decide whether the previous is smaller than current number. If it is we add 1 to nums and then we determine whether the current nums is larger than the longest, else we reset nums to 1.
Lastly, we ouput the longest.
Time complexity: O(n)
Code
Now is the code you all waiting for:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,tmp,longest=1,previous=0,nums=1;
int main(){
cin>>n;
cin>>previous;
for (int i=1;i<n;i++){
cin>>tmp;
if (tmp>previous) nums++,longest=max(nums,longest);
else nums=1;
previous=tmp;
}
cout<<longest;
return 0;
}
Enjoy 😃!