978. Longest Turbulent Subarray

Practice Longest Turbulent Subarray for free on NudgeCode: Medium, Greedy pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.

A subarray is turbulent when the comparison between neighbours keeps flipping: up, down, up, ... or down, up, down, ... (equal neighbours break it). Return the length of the longest turbulent subarray of arr. A single element counts as length 1.

Example

arr = [9,4,2,10,7,8,8,1,9]  ->  5   ([4,2,10,7,8])
arr = [4,8,12,16]           ->  2

Constraints