Algorithms · Visualized

Divide & Conquer

Divide & Conquer

Break a problem into independent subproblems, solve each, and combine the answers.

Big-O: Classic D&C recursions: Merge Sort, Quick Sort, Maximum Subarray, Fast Power.

📖 Family Introduction

Divide & Conquer

Split into independent pieces, solve each, and combine — recursion with a proof of independence.

Divide & conquer breaks a problem into a smaller versions of itself, solves them recursively, and merges the results. It differs from plain recursion by that explicit three-phase structure, and from dynamic programming by the independence of the subproblems — they must not overlap. Cost analysis uses recurrences like T(n) = a·T(n/b) + f(n); with the master theorem, merge sort’s 2T(n/2) + O(n) resolves to O(n log n).

The general recipe

  1. Base case: solve trivial sizes directly.
  2. Divide: split the input into b roughly equal, independent parts.
  3. Conquer: solve each part recursively (they do not share state).
  4. Combine: merge partial answers — this is where each algorithm’s character lives (merge, cross-sum, strip check).
  5. Analyse: write the recurrence and apply the master theorem to confirm the win over brute force.

Vocabulary you'll meet everywhere here

Recurrence
T(n) = a·T(n/b) + f(n): a subproblems of size n/b plus combine cost f(n)
Master theorem
closed-form solution of that recurrence for common shapes of f(n)
Combine step
the merge/cross/strip computation that stitches sub-answers together
Independence
subproblems must not overlap — otherwise memoized DP is the right tool
Balance
uneven splits (quicksort’s worst case) degrade guarantees to O(n²)

Where this family shows up

  • Merge sort and quicksort — the canonical split/combine and partition/recurse designs
  • Karatsuba multiplication (3 half-size multiplications, O(n^1.585)) and Strassen matrix multiplication (7 instead of 8, O(n^2.807))
  • Counting inversions during a merge, closest pair of points via the strip trick, maximum subarray via cross-sums

Common pitfalls

  • Subproblems that overlap (same inputs solved twice) belong to dynamic programming, not D&C
  • An expensive combine step can erase the recursion’s gains — measure it
  • Unbalanced splits (1 and n−1) collapse O(n log n) promises to quadratic
  • Wrong base-case size (recursing below size 1) causes infinite recursion or wasted calls
Python · Divide & Conquer in Code
def max_subarray(a, lo, hi):
    if lo == hi:
        return a[lo]
    mid = (lo + hi) // 2
    left = max_subarray(a, lo, mid)
    right = max_subarray(a, mid + 1, hi)
    s = best = a[mid]
    for i in range(mid - 1, lo - 1, -1):
        s += a[i]
        best = max(best, s)
    s2 = best2 = a[mid + 1]
    for i in range(mid + 2, hi + 1):
        s2 += a[i]
        best2 = max(best2, s2)
    return max(left, right, best + best2)

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Keep the momentum going

Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.