Skip to main content

Longest Turbulent Subarray - Solution & Explanation

MediumArrayDynamic ProgrammingSliding Window14 min readAsked at: Amazon, Microsoft, Google
Practice this problem

Problem Statement

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] when k is odd, and
    • arr[k] < arr[k + 1] when k is even.
  • Or, for i <= k < j:
    • arr[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[3] > 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

Approach Overview

Problem Overview: You are given an integer array and need the length of the longest turbulent subarray. A subarray is turbulent when comparison signs alternate between adjacent elements (e.g., a[i] > a[i+1] < a[i+2] > a[i+3]). The task is to scan the array and find the maximum length segment where this alternating pattern holds.

Approach 1: Sliding Window (O(n) time, O(1) space)

This approach treats the problem as a variable-length window over the array. Iterate through the array and compare each pair of adjacent elements. Maintain a window where the comparison sign alternates between > and <. If the pattern breaks (equal elements or repeated comparison direction), reset the window start to the previous index. Each step updates the current window length and the global maximum. Because each element is processed once and the window moves forward without backtracking, the time complexity is O(n) with constant O(1) space.

The key insight is tracking the sign of the last comparison. When the current comparison multiplied by the previous comparison is -1, the alternating pattern continues. Otherwise the turbulent sequence must restart. This makes sliding window a natural fit for the problem.

Approach 2: Dynamic Programming (O(n) time, O(n) space)

The dynamic programming approach tracks two states for every index: the longest turbulent subarray ending at that index with the last comparison being >, and the longest ending with <. If arr[i] > arr[i-1], extend the sequence that previously ended with <. If arr[i] < arr[i-1], extend the sequence that ended with >. Otherwise both states reset to length 1.

You can implement this with two arrays up[] and down[], or reduce memory to two variables. Each step updates the current state based on the previous index. The algorithm still scans the array once, giving O(n) time complexity. The straightforward DP version uses O(n) space, though it can be optimized to O(1).

Recommended for interviews: The sliding window solution is what interviewers usually expect. It directly models the alternating comparison constraint and achieves optimal O(n) time with constant memory. Explaining the DP state transition first can demonstrate reasoning about alternating relationships, but implementing the sliding window shows stronger practical problem‑solving and familiarity with common array patterns.

Approach 1: Sliding Window

In this approach, a sliding window technique is used to identify the longest turbulent subarray. We maintain two pointers: 'start' and 'end'. Initially, 'start' is set to 0, and 'end' is used to iterate over the array. At each step, we check the relationship between consecutive elements to determine the turbulence. When the pattern breaks, we calculate the length of the turbulent segment and adjust the pointers accordingly.

This C implementation iterates over the array, checking each adjacent pair of elements to determine if they are part of a turbulent sequence. It adjusts the length of the current turbulent subarray accordingly and keeps track of the maximum found so far.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the length of the array, as the solution involves a single pass over the array.
Space Complexity: O(1), as it uses a constant amount of extra space.

Try this approach in the editor →

Approach 2: Dynamic Programming

This approach utilizes dynamic programming to maintain two arrays: inc and dec, which record the lengths of increasing and decreasing turbulent subarrays that end at each index. The solution updates these values based on the conditions of the current element relative to the previous one, and tracks the global maximum using the values in inc and dec.

This C implementation uses two variables, inc and dec, to manage the lengths of turbulent subarrays. During each iteration, they are updated based on comparisons between current and previous elements.

Code

C

C++

Java

Python

C#

JavaScript

Go

TypeScript

Rust

Complexity

Time Complexity: O(n), as the array is traversed once.
Space Complexity: O(1), minimal additional space is used.

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Sliding Window

Time Complexity: O(n), where n is the length of the array, as the solution involves a single pass over the array.
Space Complexity: O(1), as it uses a constant amount of extra space.

Dynamic Programming

Time Complexity: O(n), as the array is traversed once.
Space Complexity: O(1), minimal additional space is used.

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Sliding WindowO(n)O(1)Best general solution. Minimal memory and straightforward single-pass logic.
Dynamic ProgrammingO(n)O(n) (or O(1) optimized)Useful for understanding alternating state transitions or when explaining the pattern step-by-step.

Video Solution

Longest Turbulent Array - Leetcode 978 - Python • NeetCodeIO • 24,179 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Longest Turbulent Subarray easy or hard?
Longest Turbulent Subarray is classified as a Medium difficulty problem. The challenge lies in correctly handling alternating comparisons and resetting the window when the pattern breaks. Once you recognize the sliding window pattern, the implementation becomes straightforward.
Longest Turbulent Subarray Python/Java solution
Both Python and Java implementations follow the same sliding window logic. Track the sign of the previous comparison and update the window length as you iterate. The code typically uses a single loop and a few variables, resulting in O(n) time and O(1) space.
How to solve Longest Turbulent Subarray in O(n)?
Iterate through the array and compute the comparison between consecutive elements. Track the previous comparison sign and extend the current window when the sign alternates. If the sign repeats or elements are equal, reset the window start. Maintain the maximum window length during the scan.
What is the best approach for Longest Turbulent Subarray?
The sliding window approach is the most efficient and commonly expected solution. It scans the array once while tracking whether adjacent comparisons alternate between greater and smaller. This produces an O(n) time complexity with O(1) extra space, making it optimal for large arrays.
Is Longest Turbulent Subarray asked at Google/Amazon/Meta?
Longest Turbulent Subarray appears in interview preparation lists for companies that emphasize array and pattern-detection problems. Variants of alternating comparison or longest pattern subarray questions have been reported in interviews at companies like Amazon and Google.
What data structure is used in Longest Turbulent Subarray?
The problem mainly uses arrays and simple integer comparisons. The optimal approach applies the sliding window technique over the array, while an alternative solution uses dynamic programming states such as 'up' and 'down' arrays to track alternating comparisons.
What is the time complexity of Longest Turbulent Subarray?
The optimal solution runs in O(n) time because the array is traversed once while maintaining a current turbulent window. Each element contributes to at most one extension or reset of the window. Space complexity is O(1) for the sliding window implementation.

Ready to solve this problem?

Practice Longest Turbulent Subarray with our built-in code editor and test cases.

Practice on FleetCode