Maximum length of Strictly Increasing or Strictly Decreasing Subarray
You are given an array of integers nums
. Your task is to find the length of the longest strictly increasing or strictly decreasing contiguous subarray.
nums = [2, 5, 7, 6, 4]
3
[2]
, [5]
, [7]
, [6]
, [4]
, [2,5]
, [5,7]
, [2,5,7]
[2]
, [5]
, [7]
, [6]
, [4]
, [7,6]
, [6,4]
, [7,6,4]
[7,6,4]
(length = 3)nums = [8, 8, 8, 8]
1
nums = [9, 7, 5, 3, 1]
5
[9]
, [7]
, [5]
, [3]
, [1]
, [9,7]
, [7,5]
, [5,3]
, [3,1]
, [9,7,5]
, [7,5,3]
, [5,3,1]
, [9,7,5,3]
, [7,5,3,1]
, [9,7,5,3,1]
[9,7,5,3,1]
(length = 5)nums = [12, 14, 16, 13, 11, 9]
4
[12]
, [14]
, [16]
, [13]
, [11]
, [9]
, [12,14]
, [14,16]
, [12,14,16]
[12]
, [14]
, [16]
, [13]
, [11]
, [9]
, [16,13]
, [13,11]
, [11,9]
, [16,13,11]
, [13,11,9]
, [16,13,11,9]
[16,13,11,9]
(length = 4)nums = [1, 3, 5, 7, 9]
5
[1]
, [3]
, [5]
, [7]
, [9]
, [1,3]
, [3,5]
, [5,7]
, [7,9]
, [1,3,5]
, [3,5,7]
, [5,7,9]
, [1,3,5,7]
, [3,5,7,9]
, [1,3,5,7,9]
[1,3,5,7,9]
(length = 5)Loading component...
Loading component...
Main Function is not defined.
INPUT: nums = [2, 5, 7, 6, 4]
OUTPUT: 3
int icount = 1;
int dcount =1 ;
int ans = 1;
for(int i =0; i <nums.length-1;i++){
if(nums[i] < nums[i+1] ){
icount++;
}//If End
else{
icount= 1;
}//Else End
if(nums[i] > nums[i+1] ){
dcount++;
}//If End
else{
dcount= 1;
}//Else End
ans = Math.max(icount, ans);
ans = Math.max(dcount, ans);
}//Loop End
return ans;
}//function end
Utility Function is not required.