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.

Intermediate 3 min readLesson 6 of 18 Interview Q&A

Divide and conquer has three steps:

  1. Divide the problem into smaller independent subproblems.
  2. Conquer each subproblem recursively (base case: solve tiny ones directly).
  3. 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.

dividedivideconquerconquerProblem (n)Sub (n/2)Sub (n/2)Combine
Divide, conquer recursively, combine.

The master theorem

Many D&C algorithms have a running time of the form

T(n)=a T ⁣(nb)+O(nd)T(n) = a\,T\!\left(\frac{n}{b}\right) + O(n^d)

a subproblems, each of size n/b, plus O(nᵈ) work to divide and combine. Compare d with log⁡ba\log_b a:

CaseConditionResultIntuition
1d>log⁡bad > \log_b aO(nd)O(n^d)Top-level work dominates
2d=log⁡bad = \log_b aO(ndlog⁡n)O(n^d \log n)Every level does equal work
3d<log⁡bad < \log_b aO(nlog⁡ba)O(n^{\log_b a})The leaves dominate
AlgorithmRecurrencea, b, dResult
Binary searchT(n/2) + O(1)1, 2, 0O(log n)
Merge sort2T(n/2) + O(n)2, 2, 1O(n log n)
Tree traversal2T(n/2) + O(1)2, 2, 0O(n)
Karatsuba3T(n/2) + O(n)3, 2, 1O(n^1.585)
Strassen7T(n/2) + O(n²)7, 2, 2O(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;
}

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.
Key takeaways
  • 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.
Interview questions
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.