← Two Pointers

Pattern Recognition Drill

#2 — Left rotate array by k

Easy Arrays & Strings

The Problem

Given array a and integer k, return the left-rotated array.

What approach would you use?

Think about it before scrolling down.

Key Signals

Two Pointers

3-reverse trick: reverse [0:k], reverse [k:n], reverse all. O(n) time, O(1) space.

Alt: Sliding Window

Slicing a[k:] + a[:k] is clean but creates a new array — fine if in-place isn't required.

Common Trap

Naive shift-one-at-a-time loop is O(n*k) — way too slow for large k.

← #1 #3 →