Tap here to switch tabs
Problem
Submissions

Problem: Longest Turbulent Subarray

med
30 min
Try to solve the Longest Turbulent Subarray problem.

Statement

Given an integer array arr, return the maximum length of a turbulent subarray of arr.

A subarray is considered turbulent if the comparison sign alternates between each consecutive pair of elements throughout the subarray.

Formally, a subarray arr[i...j] is turbulent if for every index kk satisfying i≤k<ji \le k < j:
• arr[k] > arr[k + 1] for odd kk and arr[k] < arr[k + 1] for even kk, or
• arr[k] < arr[k + 1] for odd kk and arr[k] > arr[k + 1] for even kk.

Constraints:

  • 1≤1 \le arr.length ≤4⋅104\le 4 \cdot 10^4

  • 0≤0 \le arr[i] ≤109\le 10^9

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Longest Turbulent Subarray

med
30 min
Try to solve the Longest Turbulent Subarray problem.

Statement

Given an integer array arr, return the maximum length of a turbulent subarray of arr.

A subarray is considered turbulent if the comparison sign alternates between each consecutive pair of elements throughout the subarray.

Formally, a subarray arr[i...j] is turbulent if for every index kk satisfying i≤k<ji \le k < j:
• arr[k] > arr[k + 1] for odd kk and arr[k] < arr[k + 1] for even kk, or
• arr[k] < arr[k + 1] for odd kk and arr[k] > arr[k + 1] for even kk.

Constraints:

  • 1≤1 \le arr.length ≤4⋅104\le 4 \cdot 10^4

  • 0≤0 \le arr[i] ≤109\le 10^9