#978
Longest Turbulent Subarray
specialist · 895 · lc medium +31 · verified · 48.9% accepted · 2,115 likes · top 35%
Description
A subarray of arr is turbulent if the comparison sign between consecutive element pairs strictly alternates (up-down or down-up). Return the maximum length of any contiguous turbulent subarray of arr.
Example 1:
Input: arr = [9,4,2,10,7,8,8,1,9]
Output: 5
Explanation: arr[1] > arr[2] < arr[3] > arr[4] < arr[5]
Example 2:
Input: arr = [4,8,12,16]
Output: 2
Example 3:
Input: arr = [100]
Output: 1
Code
1
2
3