978. Longest Turbulent Subarray
Longest Turbulent Subarray (LeetCode 978) 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.
How to solve Longest Turbulent Subarray
Longest Turbulent Subarray is a Variable Size Sliding Window problem, part of Sliding Window. Master variable-size sliding window for condition-based problems like longest substring without repeating characters.
Work towards the solution in these steps:
- Examine the direction of each comparison between adjacent values.
- When the next comparison has the opposite direction from the previous one, the current turbulent range can be extended.
- An equality immediately ends the current turbulent range, while two consecutive comparisons with the same direction require starting a new range from the previous element.
The reference solution runs in O(n) time and O(1) space.
The Longest Turbulent Subarray solution and editorial walks through the full approach with reference code.
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) 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:
- Contains Duplicate II — Easy, same subpattern
- Continuous Subarrays — Medium, same subpattern
- Find Longest Special Substring That Occurs Thrice I — Medium, same subpattern
- Frequency of the Most Frequent Element — Medium, same subpattern
- Fruit Into Baskets — Medium, same subpattern
- Jump Game VII — Medium, same subpattern
Longest Turbulent Subarray FAQ
What is the LeetCode number of Longest Turbulent Subarray?
Longest Turbulent Subarray is LeetCode problem 978, rated Medium.
What pattern is used to solve Longest Turbulent Subarray?
Longest Turbulent Subarray is a Variable Size Sliding Window problem. It belongs to the Sliding Window pattern, in the Variable Size (Condition-Based) subpattern.
What is the time complexity of Longest Turbulent Subarray?
The reference solution runs in O(n) time and O(1) space.