Divide & Conquer and the Master Theorem
Split a problem into independent subproblems, solve them recursively, combine — and analyse it in one line with the master theorem.
Divide and conquer has three steps:
- Divide the problem into smaller independent subproblems.
- Conquer each subproblem recursively (base case: solve tiny ones directly).
- Combine the sub-answers into the answer.
Merge sort, quicksort, binary search, fast exponentiation, Karatsuba multiplication, Strassen's matrix multiplication and the FFT are all divide and conquer.
The master theorem
Many D&C algorithms have a running time of the form
a subproblems, each of size n/b, plus O(nᵈ) work to divide and combine. Compare d with :
| Case | Condition | Result | Intuition |
|---|---|---|---|
| 1 | Top-level work dominates | ||
| 2 | Every level does equal work | ||
| 3 | The leaves dominate |
| Algorithm | Recurrence | a, b, d | Result |
|---|---|---|---|
| Binary search | T(n/2) + O(1) | 1, 2, 0 | O(log n) |
| Merge sort | 2T(n/2) + O(n) | 2, 2, 1 | O(n log n) |
| Tree traversal | 2T(n/2) + O(1) | 2, 2, 0 | O(n) |
| Karatsuba | 3T(n/2) + O(n) | 3, 2, 1 | O(n^1.585) |
| Strassen | 7T(n/2) + O(n²) | 7, 2, 2 | O(n^2.807) |
Example: maximum subarray, three ways
Find the contiguous subarray with the largest sum.
- Brute force — all O(n²) subarrays.
- Divide & conquer — the best subarray lies entirely in the left half, entirely in the right half, or crosses the middle (best suffix of left + best prefix of right, O(n)). T(n) = 2T(n/2) + O(n) = O(n log n).
- Kadane's algorithm (DP) — O(n): at each index, either extend the previous subarray or start fresh.
// Divide & conquer: O(n log n)
int maxSub(int[] a, int l, int r) {
if (l == r) return a[l];
int m = (l + r) >>> 1;
int leftBest = Integer.MIN_VALUE, s = 0;
for (int i = m; i >= l; i--) { s += a[i]; leftBest = Math.max(leftBest, s); }
int rightBest = Integer.MIN_VALUE; s = 0;
for (int i = m + 1; i <= r; i++) { s += a[i]; rightBest = Math.max(rightBest, s); }
return Math.max(Math.max(maxSub(a, l, m), maxSub(a, m + 1, r)), leftBest + rightBest);
}
// Kadane: O(n)
int kadane(int[] a) {
int best = a[0], cur = a[0];
for (int i = 1; i < a.length; i++) {
cur = Math.max(a[i], cur + a[i]); // extend or restart
best = Math.max(best, cur);
}
return best;
}def max_sub(a, l, r): # divide & conquer: O(n log n)
if l == r:
return a[l]
m = (l + r) // 2
s, left_best = 0, float("-inf")
for i in range(m, l - 1, -1):
s += a[i]; left_best = max(left_best, s)
s, right_best = 0, float("-inf")
for i in range(m + 1, r + 1):
s += a[i]; right_best = max(right_best, s)
return max(max_sub(a, l, m), max_sub(a, m + 1, r), left_best + right_best)
def kadane(a): # O(n)
best = cur = a[0]
for x in a[1:]:
cur = max(x, cur + x) # extend or restart
best = max(best, cur)
return bestfunction kadane(a: number[]): number { // O(n)
let best = a[0], cur = a[0];
for (let i = 1; i < a.length; i++) {
cur = Math.max(a[i], cur + a[i]); // extend or restart
best = Math.max(best, cur);
}
return best;
}Classic D&C algorithms worth knowing
- Quickselect — k-th smallest in average O(n): partition, recurse into one side only.
- Counting inversions — merge sort that counts how many left-half elements exceed each right-half element during the merge: O(n log n).
- Closest pair of points — sort by x, split, recurse, then check a narrow strip around the dividing line: O(n log n).
- Karatsuba multiplication — multiply two n-digit numbers with 3 half-size multiplications instead of 4.
- Fast Fourier Transform — polynomial multiplication in O(n log n); used in signal processing and big-integer arithmetic.
- Divide into independent subproblems, conquer recursively, combine.
- Write the recurrence; solve
T(n) = aT(n/b) + O(nᵈ)with the master theorem. - If subproblems overlap, switch to dynamic programming.
QCount inversions in an array (pairs i < j with a[i] > a[j]).hard
Modified merge sort: when merging, each time you take an element from the right half, every remaining element in the left half forms an inversion with it — add mid − i + 1. O(n log n).
QApply the master theorem to T(n) = 4T(n/2) + n.medium
a = 4, b = 2, d = 1. log₂ 4 = 2 > 1, so case 3: O(n²). The four half-size subproblems dominate.