Divide & Conquer
The Mental Model
Divide and Conquer is the recursive halving strategy. Instead of solving a problem of size n directly, split it into smaller subproblems (usually halves), solve each recursively, and combine the results. The power comes from reducing O(n^2) to O(n log n). Analogy: Imagine you need to count a huge pile of coins. Instead of counting one by one (O(n)), you split the pile in half, ask two friends to each count their half, and add the results. Each friend does the same—splits their pile and delegates. At the bottom, someone has one coin and just says “1.” The work at each level is O(n) (combining), and there are O(log n) levels, giving O(n log n) total.Pattern Recognition Signals:
- Problem can be split into independent subproblems
- Combining halves is cheaper than solving directly
- “Merge” or “split” appears naturally
- O(n log n) is needed but brute force is O(n²)
The D&C Template
Pattern 1: Merge Sort
The classic D&C algorithm. Demonstrates the paradigm perfectly.- Divide: O(1)
- Conquer: 2 × T(n/2)
- Combine: O(n)
- Total: T(n) = 2T(n/2) + O(n) = O(n log n)
Pattern 2: Count Inversions
Problem: Count pairs (i, j) where i < j but arr[i] > arr[j]. Insight: Modified merge sort. During merge, when we pick from the right half, all remaining left elements form inversions with it. Why this works: After dividing and sorting each half, elements within each half are already sorted (no internal inversions left to count—they were counted in deeper recursive calls). The only inversions that remain are cross-boundary pairs: an element from the left half that is larger than an element from the right half. During the merge step, whenever we pickarr[j] from the right half because arr[j] < arr[i], all elements from arr[i] to arr[mid] are also greater than arr[j]—that is mid - i + 1 inversions in one shot.
Pattern 3: Maximum Subarray (Kadane Alternative)
Problem: Find contiguous subarray with maximum sum. D&C Approach: Max subarray is either entirely in left, entirely in right, or crosses the middle.Pattern 4: Closest Pair of Points
Problem: Given n points in 2D, find the pair with minimum distance. Brute Force: O(n²) - check all pairs. D&C Approach:- Sort by x-coordinate
- Split into left and right halves
- Recursively find closest pair in each half
- Check pairs crossing the middle (the tricky part)
Pattern 5: Quick Select (Kth Smallest)
Problem: Find kth smallest element in O(n) average time. Approach: Partition like quicksort, but only recurse into the relevant half. This is the “half-lazy quicksort” — you do one partition, then only explore the side containing the answer. Why it is O(n) on average: After partitioning, you discard roughly half the array. The work at each level sums to n + n/2 + n/4 + … = 2n = O(n). Compare to quicksort, which must recurse into both halves for O(n log n) total. Worst case: O(n^2) if the pivot is consistently the worst element (e.g., already sorted array with last-element pivot). Randomizing the pivot or using the “median of medians” algorithm guarantees O(n) worst case, but in CP the randomized version is almost always sufficient.The Master Theorem
For recurrences of the form T(n) = aT(n/b) + f(n):
Common Examples:
- Merge Sort: T(n) = 2T(n/2) + O(n) → O(n log n)
- Binary Search: T(n) = T(n/2) + O(1) → O(log n)
- Strassen: T(n) = 7T(n/2) + O(n²) → O(n^2.81)
Common Mistakes
D&C vs Other Paradigms
Practice Problems
Beginner (800-1100)
Intermediate (1100-1400)
Advanced (1400-1700)
Key Takeaways
Split in Half
Most D&C algorithms split the problem into two equal halves.
O(n log n)
The magic of D&C: reduce O(n²) to O(n log n).
Combine Efficiently
The combine step must be O(n) or better for efficiency.
Master Theorem
Use it to analyze D&C recurrences quickly.
Next Up
Chapter 11: Dynamic Programming Fundamentals
Enter the world of DP—the most powerful technique in competitive programming.