> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Longest Turbulent Subarray Python Solution

> Tested Python solution for LeetCode 978 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 978, Medium. Topics: Array, Dynamic Programming, Sliding Window. [View on LeetCode](https://leetcode.com/problems/longest-turbulent-subarray/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 978   # by problem number
lcpy gen -s longest_turbulent_subarray   # by problem name
```

## 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]` 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.

### Examples

```
Input: arr = [9,4,2,10,7,8,8,1,9]
Output: 5
Explanation: arr[1] > arr[2] < arr[3] > arr[4] < arr[5]
```

```
Input: arr = [4,8,12,16]
Output: 2
```

```
Input: arr = [100]
Output: 1
```

### Constraints

* 1 \<= arr.length \<= 4 \* 10^4
* 0 \<= arr\[i] \<= 10^9

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/longest_turbulent_subarray/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/longest_turbulent_subarray/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def max_turbulence_size(self, arr: list[int]) -> int:
        n = len(arr)
        best = 1
        left = 0
        last_sign = 0  # 1 for prev < next, -1 for prev > next, 0 for equal

        for right in range(1, n):
            if arr[right - 1] < arr[right]:
                sign = 1
            elif arr[right - 1] > arr[right]:
                sign = -1
            else:
                sign = 0

            if sign == 0:
                best = max(best, right - left)
                left = right
                last_sign = 0
            elif sign == last_sign:
                best = max(best, right - left)
                left = right - 1
                last_sign = sign
            else:
                best = max(best, right - left + 1)
                last_sign = sign

        return best
```

## Complexity

| Time | Space |
| ---- | ----- |
| O(n) | O(1)  |

## Tags

[NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
