Longest Turbulent Subarray
MediumUpdated: Aug 2, 2025
Practice on:
Problem
Given an integer array arr, return the length of a maximum size turbulent subarray of arr.
A subarray is turbulent if the comparison sign flips between each adjacent pair of elements in the subarray.
More formally, a subarray [arr[i], arr[i + 1], ..., arr[j]] of arr is said to be turbulent if and only if:
- For
i <= k < j:arr[k] > arr[k + 1]whenkis odd, andarr[k] < arr[k + 1]whenkis even.
- Or, for
i <= k < j:arr[k] > arr[k + 1]whenkis even, andarr[k] < arr[k + 1]whenkis odd.
Examples
Example 1
Input: arr = [9,4,2,10,7,8,8,1,9]
Output: 5
Explanation: arr[1] > arr[2] < arr[3] > arr[4] < arr[5]
Example 2
Input: arr = [4,8,12,16]
Output: 2
Example 3
Input: arr = [100]
Output: 1
Solution
Method 1 – Sliding Window
Intuition
We use two pointers to maintain a window where the comparison sign flips at every step. If the current comparison does not flip, reset the window.
Approach
- Initialize two pointers and a variable for the answer.
- For each element, check the comparison sign with the previous element.
- If the sign flips, extend the window; otherwise, reset the window.
- Track the maximum window size.
Code
C++
class Solution {
public:
int maxTurbulenceSize(vector<int>& arr) {
int n = arr.size(), ans = 1, l = 0;
for (int r = 1; r < n; ++r) {
if (arr[r-1] == arr[r]) l = r;
else if (r == 1 || (arr[r-2] < arr[r-1] && arr[r-1] > arr[r]) || (arr[r-2] > arr[r-1] && arr[r-1] < arr[r])) {
// continue
} else l = r-1;
ans = max(ans, r-l+1);
}
return ans;
}
};
Go
func maxTurbulenceSize(arr []int) int {
n, ans, l := len(arr), 1, 0
for r := 1; r < n; r++ {
if arr[r-1] == arr[r] {
l = r
} else if r == 1 || (arr[r-2] < arr[r-1] && arr[r-1] > arr[r]) || (arr[r-2] > arr[r-1] && arr[r-1] < arr[r]) {
// continue
} else {
l = r-1
}
if r-l+1 > ans {
ans = r-l+1
}
}
return ans
}
Java
class Solution {
public int maxTurbulenceSize(int[] arr) {
int n = arr.length, ans = 1, l = 0;
for (int r = 1; r < n; ++r) {
if (arr[r-1] == arr[r]) l = r;
else if (r == 1 || (arr[r-2] < arr[r-1] && arr[r-1] > arr[r]) || (arr[r-2] > arr[r-1] && arr[r-1] < arr[r])) {
// continue
} else l = r-1;
ans = Math.max(ans, r-l+1);
}
return ans;
}
}
Kotlin
class Solution {
fun maxTurbulenceSize(arr: IntArray): Int {
val n = arr.size
var ans = 1
var l = 0
for (r in 1 until n) {
if (arr[r-1] == arr[r]) l = r
else if (r == 1 || (arr[r-2] < arr[r-1] && arr[r-1] > arr[r]) || (arr[r-2] > arr[r-1] && arr[r-1] < arr[r])) {
// continue
} else l = r-1
ans = maxOf(ans, r-l+1)
}
return ans
}
}
Python
class Solution:
def maxTurbulenceSize(self, arr: list[int]) -> int:
n, ans, l = len(arr), 1, 0
for r in range(1, n):
if arr[r-1] == arr[r]:
l = r
elif r == 1 or (arr[r-2] < arr[r-1] and arr[r-1] > arr[r]) or (arr[r-2] > arr[r-1] and arr[r-1] < arr[r]):
pass
else:
l = r-1
ans = max(ans, r-l+1)
return ans
Rust
impl Solution {
pub fn max_turbulence_size(arr: Vec<i32>) -> i32 {
let n = arr.len();
let mut ans = 1;
let mut l = 0;
for r in 1..n {
if arr[r-1] == arr[r] {
l = r;
} else if r == 1 || (arr[r-2] < arr[r-1] && arr[r-1] > arr[r]) || (arr[r-2] > arr[r-1] && arr[r-1] < arr[r]) {
// continue
} else {
l = r-1;
}
ans = ans.max(r-l+1);
}
ans
}
}
TypeScript
class Solution {
maxTurbulenceSize(arr: number[]): number {
const n = arr.length
let ans = 1, l = 0
for (let r = 1; r < n; ++r) {
if (arr[r-1] === arr[r]) l = r
else if (r === 1 || (arr[r-2] < arr[r-1] && arr[r-1] > arr[r]) || (arr[r-2] > arr[r-1] && arr[r-1] < arr[r])) {
// continue
} else l = r-1
ans = Math.max(ans, r-l+1)
}
return ans
}
}
Complexity
- ⏰ Time complexity:
O(n)— Single pass through arr. - 🧺 Space complexity:
O(1)— Only a few variables are used.