15% off all plans & roadmaps Use code
See plansProblem editorial
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.
Longest Turbulent Subarray is a Sliding Window problem, in the Variable Size (Condition-Based) subpattern. The pattern covers fixed-size, variable-size, and character frequency matching window techniques
The reference solution runs in O(n), where n is arr.size(). time and O(1). space.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Longest Turbulent Subarray is a Sliding Window problem, in the Variable Size (Condition-Based) subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.