โ† Back to section

Recognizing array problems

The goal of this section is not to memorize the solutions to 8 problems โ€” it's for you to leave here able to solve any array problem, including the hard ones you've never seen. This lesson teaches the method; the next ones teach the patterns.

Each pattern lesson starts with a ๐ŸŽฏ Try first challenge: you try to solve it before seeing the theory. Don't skip it โ€” feeling the difficulty is what makes the idea click afterward. (If you haven't seen the Start here section yet, it's worth starting there.)

Why array is the first section

Almost every LeetCode problem passes through an array at some point. The five patterns you'll learn here โ€” hash map, two pointers, sliding window, prefix and Kadane โ€” reappear in disguise in strings, matrices, intervals and even in trees. Mastering this section is mastering the foundation.

The general method (works for any problem)

Faced with any array statement, follow these 4 steps. They are the "algorithm for choosing the algorithm":

Step 1 โ€” Solve it by hand first

Take the example from the statement and solve it by hand, without code. If you can't solve it with pen and paper, no pattern will save you. This step also reveals the special cases: empty array? a single element? negative numbers? duplicates?

Step 2 โ€” Write the brute force (mentally)

What is the dumb solution that tries everything? It's almost always "for each element, look at all the others" โ€” two loops, O(nยฒ). It's your starting point, not your shame: in an interview, stating the brute force first shows you understood the problem.

Step 3 โ€” Find the waste

This is the golden question: what work does the brute force repeat needlessly? Each pattern in the section eliminates a specific kind of waste:

The brute force's wasteThe pattern that eliminates it
"I search for the same value many times"Hash map โ€” remember what you've seen
"I compare pairs that obviously can't be the answer"Two pointers โ€” discard candidates in bulk
"I recompute the whole window that barely changed"Sliding window โ€” update only the edge
"I re-sum the same range many times"Prefix โ€” precompute running totals
"I re-evaluate every subarray start"Kadane โ€” decide locally: extend or restart

Step 4 โ€” Check against the limits

The statement always tells you the maximum input size โ€” and that dictates the complexity you need (remember the Big-O table?):

Limit on nRequired complexityTranslation
n โ‰ค ~500O(nยฒ) or even O(nยณ) passesbrute force may be enough
n โ‰ค ~100,000O(n log n)there's probably a sort along the way
n โ‰ค ~1,000,000O(n) or O(n log n)you need one of the patterns

If your solution is O(nยฒ) and n goes up to 10โต, you already know the verdict before submitting: Time Limit Exceeded. Go back to step 3.

The method in action (no spoilers)

Take the classic problem: "find two numbers that add up to a target" (Two Sum). Applying the method:

  1. By hand: [2, 7, 11, 15], target 9 โ†’ 2 + 7 = 9. Easy manually: you look at one number and search for the one that's missing.
  2. Brute force: for each nums[i], scan the rest looking for target - nums[i]. Two loops, O(nยฒ).
  3. Waste: on each pass I search again, from scratch, for a value in the array โ€” even though I already went past it! If I remembered what I've seen, the search would be instant.
  4. Limits: n up to 10โด โ€” O(nยฒ) barely passes, but the interview follow-up is certain: "can you do it in a single pass?"

"Remember what I've seen with instant lookup" has a name: hash map โ€” the next lesson. Every pattern lesson in this section follows this same trail: first the waste, then the idea that eliminates it, then the problems where it shines.

The 8 problems of the section

These are the Top 75 problems we'll use as examples of the patterns (the goal is the pattern, not the problem):

  • Two Sum and Contains Duplicate โ†’ hash map
  • Container With Most Water and 3Sum โ†’ two pointers
  • Best Time to Buy and Sell Stock โ†’ sliding window / running minimum
  • Product of Array Except Self โ†’ prefix and suffix
  • Maximum Subarray and Maximum Product Subarray โ†’ Kadane

And on the final exam: three brand-new problems for you to apply all of this without a safety net.

๐Ÿง  Review quiz

0/3

1. What is the 'golden question' of step 3 of the method, after thinking about the brute force?

2. The statement says n โ‰ค 100,000 and your solution is O(nยฒ). What do you conclude BEFORE submitting?

3. In a problem, you notice the brute force keeps searching, repeatedly, whether a value exists in the array. Which pattern attacks exactly that waste?

Summary

  • The method: solve by hand โ†’ brute force โ†’ find the waste โ†’ check the limits.
  • The statement's limits dictate the required complexity.
  • The 5 patterns of the section are 5 answers to 5 different wastes.

Next lesson: the most-used pattern in all of LeetCode โ€” hash map.