Common Patterns

**Pattern 1: Boundary Finding or Counting Occurrences

What it finds: The first/last position where some condition changes from false to true (or vice versa) When to use: Array has a clear “split point” where everything on one side satisfies a condition and everything on the other side doesn’t example problems:

Pattern 1.1: Peak/Valley Finding

What it finds: Local maximum/minimum where comparison with neighbors determines search direction

When to use:

What it finds: The optimal value from a range of possible answers by testing if each candidate works

When to use:

  • Problem asks for “minimum X such that…” or “maximum X such that…”
  • You can write a function to check if a candidate answer is valid
  • The validity follows a monotonic pattern (if X works, then X+1 also works, or vice versa)

What it finds: The optimal value from a range of possible answers by testing if each candidate works

When to use:

  • Problem asks for “minimum X such that…” or “maximum X such that…”
  • You can write a function to check if a candidate answer is valid
  • The validity follows a monotonic pattern (if X works, then X+1 also works, or vice versa)

**Pattern 4: sqr root and other roots

The square root of n is the number r such that r2 = n. Square root computations are performed inside every pocket calculator, but it is instructive to develop an efficient algorithm to compute them. First, observe that the square root of n ≥ 1 must be at least 1 and at most n. Let l = 1 and r = n. Consider the midpoint of this interval, m = (l + r)/2. How does m2 compare to n? If n ≥ m2, then the square root must be greater than m, so the algorithm repeats with

problem solved so far from leetcode