Longest Turbulent Subarray

Longest Turbulent Subarray is a Medium Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Sliding Window pattern, in the Variable Size (Condition-Based) subpattern.

Problem statement

Given an integer array arr, a subarray is a contiguous, non-empty portion of the array. A subarray is turbulent when each pair of neighboring values alternates between a strict increase and a strict decrease: the comparisons must follow either > then and so on, or then , , <, so the entire array is turbulent.

Example 3:

Input: arr = [4,4,2,2,9] Output: 2 Explanation: Equal neighboring values break turbulence. The longest valid subarrays are pairs such as [4,2] or [2,9], each of length 2.

Constraints:

- 1 <= arr.length <= 4 10^4 - 0 <= arr[i] <= 10^9 - arr contains integers

Topics and companies

Topics: Array, Greedy, Sliding Window.

How to practise Longest Turbulent Subarray on Thita.ai

Starter code is provided in C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n), where n is arr.size(). time and O(1). space.

Open Longest Turbulent Subarray in the code editor, or read the Longest Turbulent Subarray editorial for a worked solution with its approach and complexity analysis.

Where Longest Turbulent Subarray sits in the DSA pattern sheet

Problems related to Longest Turbulent Subarray

Other problems that use the same Variable Size (Condition-Based) and Sliding Window techniques:

Preparing this workspace.