Longest Turbulent Subarray
MediumSliding Window
Decode it first. Clues, constraints, pattern and plan before any code.A subarray is turbulent if the comparison sign flips between each adjacent pair: up, down, up, ... or down, up, down, ... (for example a < b > c < d). Equal neighbours break it. Given arr, return the length of the longest turbulent subarray. A single element counts as turbulent.
arr = [9, 4, 2, 10, 7, 8, 8, 1, 9] → 5 ([4, 2, 10, 7, 8])
arr = [4, 8, 12, 16] → 2
arr = [100] → 1Constraints
1 <= arr.length <= 4 * 10^40 <= arr[i] <= 10^9