Algorithms · Divide and Conquer: Max-Min, Maximum Subarray, Inversions and Fast Exponentiation
The Divide-and-Conquer Template
Every divide-and-conquer (D&C) algorithm has three phases:
- Divide the problem of size into subproblems, each of size .
- Conquer each subproblem recursively (or directly, if small enough — the base case).
- Combine the sub-solutions into one solution, at cost .
The running time therefore satisfies
where = number of subproblems, = shrink factor, = divide + combine cost. This is solved by the Master Theorem (previous chapter). For the algorithms below, the recurrences are:
| Algorithm | Recurrence | Solution |
|---|---|---|
| Max-Min | comparisons | |
| Merge sort / inversion count | ||
| Maximum subarray (D&C) | ||
| Binary search | ||
| Fast exponentiation | multiplications | |
| Tower of Hanoi | moves |
Note the last one: Hanoi shrinks by a constant (), not by a factor, so the Master Theorem does not apply — expand the recurrence directly.
2.1 The naive method and its cost
Find max with comparisons, then min with comparisons: total .
2.2 The D&C method
Divide the array into two halves. Conquer to get (max, min) of each half. Combine with exactly 2 comparisons: compare the two maxima, compare the two minima.
(The base case of two elements needs 1 comparison.)
Solving the recurrence by expansion. Let and stop at the two-element base case . (The base must be ; with the same recurrence would give the naive .) Unrolling times:
The same count is obtained without recursion by the pairwise (tournament) method, which is the standard "3n/2 − 2" result:
Pairwise method. Pair up the elements: pairs, each costing 1 comparison (winner = candidate max, loser = candidate min). Then scan the candidates-max for the maximum ( comparisons) and the candidates-min for the minimum ( comparisons).
Total for even :
For odd : comparisons.
Worked check, . Pairs: 4 comparisons. Candidates-max: 4 elements → 3 comparisons. Candidates-min: 4 elements → 3 comparisons. Total . And . ✓
Worked check, . Pairs: 3 comparisons. Candidates-max: 3 → 2 comparisons. Candidates-min: 3 → 2 comparisons. Total . Formula: . ✓
Worked check, (odd). Two pairs (2 comparisons) + one leftover element. Candidates-max: 2 winners + 1 leftover = 3 elements → 2 comparisons. Candidates-min: 2 losers + 1 leftover = 3 elements → 2 comparisons. Total . Formula: . ✓
2.3 Why 3n/2 − 2 is optimal
Each element must "lose" at least once to be excluded from being max, and "win" at least once to be excluded from being min. Elements that lose a comparison can never be max; elements that win can never be min. A comparison between two elements that have both already won (or both already lost) wastes information. The pairwise scheme makes every comparison informative, achieving the lower bound .
2.4 The D&C recurrence, stated correctly
The clean D&C recurrence that yields the same count is
For : . ✓ Matches the pairwise count. For : , and . ✓
Binary Search Variants
All are D&C with one recursive call: . The array must be sorted (except in the rotated case, where it is sorted-then-rotated).
3.1 Classic search
lo = 0, hi = n-1
while lo <= hi:
mid = lo + (hi-lo)//2
if A[mid] == key: return mid
elif A[mid] < key: lo = mid+1
else: hi = mid-1
return -1
Comparisons: at most . For : .
3.2 First occurrence (lower bound)
When A[mid] == key, do not return; record ans = mid and continue left (hi = mid-1). This finds the leftmost index. Still .
3.3 Last occurrence (upper bound)
Symmetric: on equality record and go right (lo = mid+1).
3.4 Rotated sorted array
Array sorted then rotated, e.g. [4,5,6,7,0,1,2]. At each step, one half is sorted. Check which:
- If
A[lo] <= A[mid], the left half is sorted. IfA[lo] <= key < A[mid], search left; else search right. - Otherwise the right half is sorted. If
A[mid] < key <= A[hi], search right; else search left.
. With duplicates the worst case degrades to (e.g. [1,1,1,0,1]).
3.5 Peak element
A peak is an index with A[i-1] <= A[i] >= A[i+1] (boundaries count one side). If A[mid] < A[mid+1], a peak lies to the right (lo = mid+1); else to the left (hi = mid). , works on unsorted arrays.
| Variant | Precondition | Time |
|---|---|---|
| Classic | sorted | |
| First / last occurrence | sorted | |
| Rotated (distinct) | rotated sorted | |
| Rotated (duplicates) | rotated sorted | worst |
| Peak | any |