Problem Statement:
You are given an array Arr of N integers. You need to find the length of longest Peak Subarray of the given array Arr.
Peak Array: Array A of length K is called as a peak Array, if following condition holds true:
A[1]<A[2]<A[3]<….<A[K].
Formally any strictly increasing array is called a Peak Array.
Subarray: Subarray of any array is a contiguous part of the array (with length greater than zero).
Your task is to find the length of the longest peak subarray of a given array.
Note: Array of length 1 is always a Peak Array.
First line of input, single integer value NOutput Format:
Second line of input, contains N integers with space-separation for Array Arr.
Print an integer that represents the length of longest peak subarray.Sample Input:
5
3 3 9 8 4
Sample Output:
2
Solution in | C | JAVA | PYTHON |
---|
#include<stdio.h>
int main() {
int n,arr[50],i,j,len=1,maxlen=0;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&arr[i]);
}
for(i=0;i<n-1;i++){
if(arr[i]<arr[i+1]){
len++;
}else{
if(maxlen<len){
maxlen = len;
len=1;
}
}
}
printf("%d",maxlen);
return 0;
}
int main() {
int n,arr[50],i,j,len=1,maxlen=0;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&arr[i]);
}
for(i=0;i<n-1;i++){
if(arr[i]<arr[i+1]){
len++;
}else{
if(maxlen<len){
maxlen = len;
len=1;
}
}
}
printf("%d",maxlen);
return 0;
}