 All Problems
Longest Turbulent Subarray
medium
sliding window
arrays
amazon
google

Given an integer array arr, return the length of a maximum size turbulent subarray of arr. A subarray is turbulent if the comparison sign alternates between each adjacent pair of elements.

Example 1:

Input: 9 4 2 10 7 8 8 1 9
Output: 5
Explanation: [4,2,10,7,8] is turbulent.

Example 2:

Input: 4 8 12 16
Output: 2

Constraints:

  • 1 ≤ arr.length ≤ 4 × 10⁴
  • 0 ≤ arr[i] ≤ 10⁹

Input format: One line — space-separated array.

Output format: Length of longest turbulent subarray.

Run to check your code against the sample cases, or submit to run every case