978. Longest Turbulent Subarray LeetCode Solution
In this guide, you will get 978. Longest Turbulent Subarray LeetCode Solution with the best time and space complexity. The solution to Longest Turbulent Subarray problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Longest Turbulent Subarray solution in C++
- Longest Turbulent Subarray solution in Java
- Longest Turbulent Subarray solution in Python
- Additional Resources

Problem Statement of Longest Turbulent Subarray
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 arr[k + 1] when k is odd, and
arr[k] < arr[k + 1] when k is even.
Or, for i <= k arr[k + 1] when k is even, and
arr[k] < arr[k + 1] when k is odd.
Example 1:
Input: arr = [9,4,2,10,7,8,8,1,9]
Output: 5
Explanation: arr[1] > arr[2] arr[4] < arr[5]
Example 2:
Input: arr = [4,8,12,16]
Output: 2
Example 3:
Input: arr = [100]
Output: 1
Constraints:
1 <= arr.length <= 4 * 104
0 <= arr[i] <= 109
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
978. Longest Turbulent Subarray LeetCode Solution in C++
class Solution {
public:
int maxTurbulenceSize(vector<int>& arr) {
int ans = 1;
int increasing = 1;
int decreasing = 1;
for (int i = 1; i < arr.size(); ++i) {
if (arr[i] > arr[i - 1]) {
increasing = decreasing + 1;
decreasing = 1;
} else if (arr[i] < arr[i - 1]) {
decreasing = increasing + 1;
increasing = 1;
} else {
increasing = 1;
decreasing = 1;
}
ans = max({ans, increasing, decreasing});
}
return ans;
}
};
/* code provided by PROGIEZ */
978. Longest Turbulent Subarray LeetCode Solution in Java
class Solution {
public int maxTurbulenceSize(int[] arr) {
int ans = 1;
int increasing = 1;
int decreasing = 1;
for (int i = 1; i < arr.length; ++i) {
if (arr[i] > arr[i - 1]) {
increasing = decreasing + 1;
decreasing = 1;
} else if (arr[i] < arr[i - 1]) {
decreasing = increasing + 1;
increasing = 1;
} else {
increasing = 1;
decreasing = 1;
}
ans = Math.max(ans, Math.max(increasing, decreasing));
}
return ans;
}
}
// code provided by PROGIEZ
978. Longest Turbulent Subarray LeetCode Solution in Python
class Solution:
def maxTurbulenceSize(self, arr: list[int]) -> int:
ans = 1
increasing = 1
decreasing = 1
for i in range(1, len(arr)):
if arr[i] > arr[i - 1]:
increasing = decreasing + 1
decreasing = 1
elif arr[i] < arr[i - 1]:
decreasing = increasing + 1
increasing = 1
else:
increasing = 1
decreasing = 1
ans = max(ans, max(increasing, decreasing))
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.