Longest Turbulent Subarray
Medium Top 250
Problem Description
Given an integer array arr, return the length of a maximum size turbulent subarray of arr.
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:
arr[k] > arr[k+1]whenkis even, andarr[k] < arr[k+1]whenkis odd.
Formally, the comparison sign between adjacent elements must alternate on each step.
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] is turbulent.
Input: arr = [4,8,12,16] Output: 2
Input: arr = [100] Output: 1
Constraints
1 ≤ arr.length ≤ 4 * 10⁴0 ≤ arr[i] ≤ 10⁹
Check Sign Alternation with a Sliding Window
Compare adjacent elements arr[i - 1] and arr[i]. We define the “sign” of the comparison as:
1ifarr[i - 1] < arr[i]-1ifarr[i - 1] > arr[i]0ifarr[i - 1] == arr[i]
We iterate through the array starting from index 1, maintaining the length of the current turbulent subarray currentLength:
- If the sign alternates compared to the previous step (e.g. previous sign was
1and current is-1), incrementcurrentLength. - If they do not alternate but are not equal (e.g. previous sign was
1and current is also1), the new turbulent subarray starts at the previous element, so setcurrentLength = 2. - If they are equal (sign is
0), the current subarray ends, resetcurrentLength = 1.
Solution: Sign-Tracking Scan
class Solution {
public int maxTurbulenceSize(int[] arr) {
int n = arr.length;
if (n < 2) return n;
int maxLen = 1;
int currentLen = 1;
int prevSign = 0; // -1, 0, or 1
for (int i = 1; i < n; i++) {
int currentSign = Integer.compare(arr[i], arr[i - 1]);
if (currentSign == 0) {
currentLen = 1;
} else if (prevSign * currentSign < 0) { // signs are opposites
currentLen++;
} else { // signs are same, or prevSign was 0
currentLen = 2;
}
maxLen = Math.max(maxLen, currentLen);
prevSign = currentSign;
}
return maxLen;
}
}class Solution:
def maxTurbulenceSize(self, arr: list[int]) -> int:
n = len(arr)
if n < 2:
return n
max_len = 1
current_len = 1
prev_sign = 0
for i in range(1, n):
if arr[i] > arr[i - 1]:
current_sign = 1
elif arr[i] < arr[i - 1]:
current_sign = -1
else:
current_sign = 0
if current_sign == 0:
current_len = 1
elif prev_sign * current_sign < 0: # alternating signs
current_len += 1
else:
current_len = 2
max_len = max(max_len, current_len)
prev_sign = current_sign
return max_len#include <vector>
#include <algorithm>
class Solution {
public:
int maxTurbulenceSize(std::vector<int>& arr) {
int n = arr.size();
if (n < 2) return n;
int maxLen = 1;
int currentLen = 1;
int prevSign = 0;
for (int i = 1; i < n; i++) {
int currentSign = (arr[i] > arr[i - 1]) - (arr[i] < arr[i - 1]);
if (currentSign == 0) {
currentLen = 1;
} else if (prevSign * currentSign < 0) {
currentLen++;
} else {
currentLen = 2;
}
maxLen = std::max(maxLen, currentLen);
prevSign = currentSign;
}
return maxLen;
}
};Complexity Analysis:
- Time Complexity: O(N) where N is the length of
arr. - Space Complexity: O(1).