You have been given an array/list HEIGHT denoting the heights of ‘N’ adjacent hills. The hills are said to be dangerous if their heights are turbulent.
A range of hills is turbulent if the comparison sign for their heights flips between each adjacent pair of hills. For example, if HEIGHT = [1, 2, 10, 3, 5, 1, 10, 10], then the hills in index range [1, 6] are turbulent as 2 < 10 > 3 < 5 > 1 < 10.
Your task is to find the length of the longest dangerous hill range.
The first line of input contains an integer 'T' representing the number of test cases or queries to be processed. Then the test case follows.
The first line of each test case contains a single integer ‘N’ representing the size of the array/list.
The second line of each test case contains ‘N’ single space-separated non-negative integers representing the array elements.
Output Format :
For each test case, print the length of the longest dangerous hill range.
Print the output of each test case in a separate line.
Note :
You do not need to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 100
1 <= N <= 5000
0 <= HEIGHT[i] <= 10^5
Where ‘N’ is the number of elements in the array/list.
Time Limit: 1sec
2
9
9 4 2 10 7 8 8 1 9
4
1 2 1 4
5
4
For the first test case, the longest dangerous hill range is [4, 2, 10, 7, 8].
For the second test case, the longest dangerous hill range is [1, 2, 1, 4].
1
2
2 8
2
Naively find all dangerous ranges and return the longest length.
We have a simple brute force solution for this problem. We will traverse over all possible subarrays/ranges of the array and for each of them, check if it is turbulent.
The key observation here is that a hill at index h ‘i’ constitutes to a dangerous range only if it either forms
O(N^3), where N denotes the size of the given array/list.
We have total (N*(N+1))/2 subarrays and we are linearly traversing each of them. So, the overall time complexity is O(N^3).
O(1), constant space is used.