Command Palette

Search for a command to run...

Module 38

Advanced Search and Divide & Conquer

Ternary search on unimodal functions, meet in the middle for 2⁴⁰ search spaces, splitting problems in half, and the closest pair of points.

Advanced 3 lessons 7 problems ~35 min of lessons

Divide and conquer solves a problem by splitting it into independent parts, solving each, and combining the results: merge sort and quickselect (Module 15) are the classic examples. This module extends the idea in three directions.

Ternary search finds the peak of a function that rises then falls. Meet in the middle splits an exponential search into two halves of 2^(n/2) each and joins them with sorting or hashing, turning 2⁴⁰ into about 2 × 2²⁰. Geometric divide and conquer finds the closest pair of points in O(n log n). The problems practise each one, plus expression splitting and staircase search in a sorted matrix.

Best after: Binary Search, Sorting and Divide & Conquer

Where this shows up in real systems

Part 1

Learn the ideas

Part 2

Solve the problems

Work through them in order. Each one shows the pattern it teaches.

  1. Search on the slope instead of a value.

  2. Start at a corner where one move increases and the other decreases: each step discards a whole row or column.

  3. Split at every operator and combine every left result with every right result.

  4. Meet in the middle with a hash map: pair sums of two arrays against pair sums of the other two.

  5. Meet in the middle: 2²⁰ sums per half, sort one, binary search from the other.

  6. Meet in the middle grouped by how many items each half contributes.

  7. Geometric divide and conquer with the strip argument.