WORLD 2

ARRAYS

One-pass scans, sort and sweep, a leaderboard, a sliding window, running totals, two pointers and a stack.

STAGE 0 Arrays tutorial

0:00 / 0:00

The move

Walk the array once while keeping a little state instead of restarting at every position. Two pointers close in from the ends of a sorted row; a sliding window grows on the right and shrinks on the left; a running total turns any range sum into one subtraction.

Spot it
A contiguous subarray or substring, a pair in a sorted array, a range sum, or the best so far in one pass.
Cost
Each pointer moves one way, so it's O(n) time and O(1) extra space; sorting first adds O(n log n).

18 STAGES Arrays problems

☆☆☆☆☆ Clear 5 to finish this world.