Array Scans
Tactic
Array scans turn a sequence into a stream of decisions. The move is to walk left to right or right to left once, carrying the smallest piece of state that makes the next item meaningful.
The invariant is the summary of everything already seen. That summary might be a best value, a running count, a last position, a candidate answer, or a flag. If the summary is enough to answer the next step, a nested loop is probably unnecessary.
Design the scan by saying what the state means before reading nums[i], then update the answer and state in a fixed order. Many bugs come from updating the state before the answer when the current element is not supposed to compare with itself.
Value
A scan is valuable because it replaces repeated re-reading with one controlled pass. It is often the first simplification before a more named tactic appears: prefix sums are scans with checkpoints, greedy reachability is a scan with a frontier, and Kadane-style DP is a scan with a compressed state.
Direct complexity example
- Brute force: Check every start and end pair for a property: time and extra space.
- With this tactic: Carry the needed summary while reading each value once: time and usually extra space.
- Space: If the summary is a frequency table or set, the scan may spend space for the distinct values it needs to remember.
Challenges this solves
- maximum or minimum so far
- first or last occurrence tracking
- one-pass profit and reachability
- counting events while preserving order
When to use it
Use this tactic when these conditions are true:
- the answer depends on a prefix, suffix, or best-so-far value
- each element only needs information from one side
- the input order matters and sorting would destroy the meaning
- the brute force repeats the same prefix or suffix work
When not to use it
Reach for a different tactic when these warning signs appear:
- a future item can invalidate many earlier choices in a way the state cannot summarize
- the problem asks for arbitrary range queries many times and needs preprocessing
- the needed state grows into all previous pairs or all previous subarrays
Terminology clues
These prompt words often point toward this concept:
- single pass
- in one traversal
- maximum so far
- running
- previous
- last seen
- best profit
- left to right
Problems that use it
- 1. Two Sum
- 20. Valid Parentheses
- 28. Find the Index of the First Occurrence in a String
- 45. Jump Game II
- 48. Rotate Image
- 53. Maximum Subarray
- 54. Spiral Matrix
- 55. Jump Game
- 73. Set Matrix Zeroes
- 74. Search a 2D Matrix
- 84. Largest Rectangle in Histogram
- 121. Best Time to Buy and Sell Stock
- 122. Best Time to Buy and Sell Stock II
- 125. Valid Palindrome
- 128. Longest Consecutive Sequence
- 134. Gas Station
- 136. Single Number
- 152. Maximum Product Subarray
- 189. Rotate Array
- 209. Minimum Size Subarray Sum
- 217. Contains Duplicate
- 228. Summary Ranges
- 238. Product of Array Except Self
- 271. Encode and Decode Strings
- 334. Increasing Triplet Subsequence
- 387. First Unique Character in a String
- 443. String Compression
- 459. Repeated Substring Pattern
- 503. Next Greater Element II
- 704. Binary Search
- 739. Daily Temperatures
- 1071. Greatest Common Divisor of Strings
- 1249. Minimum Remove to Make Valid Parentheses
- 1899. Merge Triplets to Form Target Triplet