Shubham Sourabhthe room · director's cutplain résumé
Room 04 · The Grind — Greedy

Longest Turbulent Subarray

MEDIUM✓ CHIP-TIMEDLC #978 — FULL STATEMENT

The briefingthe full drill, in my own words

The drill: Find the longest run of consecutive elements where the comparisons zig-zag — each step must flip between climbing and dropping compared to the step before. A flat or repeated direction ends the run.

An array of integers arrives, and the goal is measuring the longest contiguous stretch where consecutive comparisons strictly alternate between climbing and dropping — up, then down, then up again, with no two same-direction steps back to back.

A stretch of just one element always counts as turbulent on its own, since there's no comparison yet to break. The moment two consecutive elements are equal, or the direction repeats instead of flipping, that particular run ends there.

Only the length of the longest such alternating stretch is needed, not its starting position or the actual values inside it.

  • the array always holds at least one element
  • values can repeat, including exact ties between neighbors
  • a single element alone always counts as a turbulent run of length one
  • only the longest run's length is reported, not its position
EX 01
arr = [4, 8, 2, 9, 1, 7, 7, 3]
→ 6
run breaks at the repeated 7
EX 02
arr = [5, 5, 5, 5]
→ 1
constant array, no direction ever changes
EX 03
arr = [1, 2, 3, 4, 5]
→ 2
strictly increasing — only adjacent pairs qualify

The hintstake only what you need

HINT 1 THE NUDGE

Checking every subarray for the alternating pattern directly re-examines the whole run each time. What could you carry forward from position i to i+1 instead of re-scanning from the start?

HINT 2 THE STRUCTURE

At each position ask only one thing: does this step continue an alternation that the OTHER direction left off, or does it break it? Two small numbers — the best run ending here going up, and the best run ending here going down — are enough to answer that.

HINT 3 ONE STEP FROM THE ANSWER

up[i] = down[i-1] + 1 when arr[i] > arr[i-1] (otherwise reset to 1); down[i] mirrors it for arr[i] < arr[i-1]. An equal pair resets both to 1. The answer is the largest value either counter ever reaches.

Coach's boardthe pattern, step by step

THE ZIGZAG COUNTERSPATTERN · ALTERNATING RUN DParr = [4, 8, 2, 9, 1, 7, 7, 3]
4
8
2
9
1
7
7
3
UP · DOWN · BEST
up → 1
down → 1
best → 1
STEP 1

Array [4, 8, 2, 9, 1, 7, 7, 3]. up and down both start at 1 — index 0 alone is a turbulent run of length one.

STEP 1 / 9 · ← → WORK TOO

The splitstwo paces, two languages

grind/longest-turbulent-subarray.py — RACE PACE
LANG ▸
PACE ▸
class Solution:
    def maxTurbulenceSize(self, arr: List[int]) -> int:
        up = down = best = 1
        for i in range(1, len(arr)):
            if arr[i] > arr[i - 1]:
                up, down = down + 1, 1
            elif arr[i] < arr[i - 1]:
                down, up = up + 1, 1
            else:
                up = down = 1
            best = max(best, up, down)
        return best
TIME O(N)SPACE O(1)PYTHON · RACE PACE · 12 LN

✓ CHIP-TIMEDALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED