Algorithms
Divide & Conquer
Divide and conquer questions are recurrence-solving questions: recognise the form T(n) = aT(n/b) + f(n), compare n^(log_b a) with f(n), and read off the answer — then handle the odd recurrences by recursion tree.
GATE CSE divide and conquer — paradigm, recurrence relations T(n) = aT(n/b) + f(n), Master theorem cases, recursion trees, merge sort, binary search, Strassen, Karatsuba and max-min with GATE samples.
Learning outcomes
- Explain the divide, conquer and combine steps and write the recurrence of a D&C algorithm
- Apply the three Master theorem cases (including the log-factor extension) to standard GATE recurrences
- Solve non-Master recurrences such as T(n) = T(n−1) + n, 2T(n−1) + 1 and T(√n) + 1 by expansion
- Quote the costs of merge sort, binary search, Strassen, Karatsuba, and D&C max–min
The divide-and-conquer paradigm
- Divide the problem into smaller subproblems of the same kind, conquer them recursively (base case solved directly), then combine the sub-solutions.
- If a problem of size n is split into a subproblems of size n/b and the divide + combine work is f(n), then T(n) = a·T(n/b) + f(n).
- Typical D&C algorithms: merge sort, quick sort, binary search, fast exponentiation, Strassen matrix multiplication, Karatsuba multiplication, closest pair of points, maximum subarray, finding max and min.
- D&C is effective when subproblems are INDEPENDENT. If subproblems overlap heavily (like Fibonacci), dynamic programming is the right tool instead.
- Space for recursive D&C includes the recursion stack: Θ(log n) for balanced halving, Θ(n) for linear recursion depth.
- Three-question checklist for writing any D&C recurrence: (1) how many subproblems a? (2) what fraction n/b of the input does each get? (3) what is the divide + combine cost f(n)? A wrong recurrence almost always misses one of these.
- The base case never changes the Θ answer (T(1) = Θ(1)), but it does change EXACT counts: max–min with T(2) = 1 gives 3n/2 − 2, while merge sort with T(1) = 0 gives n log n − n + 1.
- Quick sort is D&C with an expensive divide (partition, Θ(n)) and a free combine; its recurrence T(n) = T(k) + T(n − k − 1) + n depends on the split: Θ(n log n) when balanced, Θ(n²) when the split is 0 : n − 1.
- Recursive binary search uses Θ(log n) stack space; the iterative version uses Θ(1). GATE often asks the space of the recursive version.
| Algorithm | Divide | Combine | Recurrence | Result |
|---|---|---|---|---|
| Binary search | Pick middle, keep one half | None | T(n) = T(n/2) + 1 | Θ(log n) |
| Merge sort | Split into two halves | Merge in Θ(n) | T(n) = 2T(n/2) + n | Θ(n log n) |
| Max and min | Split into two halves | 2 comparisons | T(n) = 2T(n/2) + 2 | Θ(n) (3n/2 − 2) |
| Strassen | 7 half-size products | Θ(n²) additions | T(n) = 7T(n/2) + n² | Θ(n^2.807) |
| Karatsuba | 3 half-size products | Θ(n) additions | T(n) = 3T(n/2) + n | Θ(n^1.585) |
| Fast exponentiation | Halve the exponent | One multiply | T(n) = T(n/2) + 1 | Θ(log n) |
Worked example
Problem. Write the recurrence of an algorithm that splits an input of size n into 3 parts of size n/3 and spends Θ(n²) combining.
Solution. T(n) = 3T(n/3) + Θ(n²).
Master theorem (GATE form)
- For T(n) = a·T(n/b) + Θ(n^k log^p n) with a ≥ 1, b > 1, k ≥ 0 and constant p: compare a with b^k (equivalently log_b a with k).
- Case 1: a > b^k (log_b a > k) ⇒ T(n) = Θ(n^(log_b a)). The leaves dominate.
- Case 2: a = b^k ⇒ T(n) = Θ(n^k log^(p+1) n). For p = 0 this is Θ(n^k log n). Every level costs about the same.
- Case 3: a < b^k ⇒ T(n) = Θ(n^k log^p n) (for p ≥ 0). The root dominates.
- The theorem needs a constant, a positive subproblem count, equal-size subproblems n/b, and a polynomially related f(n). It does NOT apply to T(n) = 2^n T(n/2) + n^n, T(n) = T(n−1) + n, or unequal splits like T(n/3) + T(2n/3).
- If f(n) is not polynomially larger/smaller than n^(log_b a) (for example n/log n against n), the basic Master theorem fails; use the recursion tree or the extended form.
- Useful exponents to memorise: log₂ 2 = 1, log₂ 4 = 2, log₂ 8 = 3, log₃ 9 = 2, log₂ 3 ≈ 1.585, log₂ 7 ≈ 2.807, log₄ 2 = 1/2. Compute n^(log_b a) FIRST, then compare it with f(n).
- Why Case 1 gives n^(log_b a): the recursion tree has a^(log_b n) = n^(log_b a) leaves, and in Case 1 the leaf level costs more than the level above it, so the leaves dominate.
- Fractional k is allowed: T(n) = 2T(n/4) + √n has a = 2, b = 4, k = 1/2, b^k = 2 = a, so Case 2 applies and T(n) = Θ(√n log n).
- Lower-order terms, constants and floors/ceilings do not matter: T(n) = 2T(⌊n/2⌋) + 5n + 7 is still Θ(n log n).
- Case 3 formally needs the regularity condition a·f(n/b) ≤ c·f(n) for some c < 1. For f(n) = n^k with a < b^k it holds automatically, so GATE never makes you check it.
Formulas to remember
-
a > b^k ⇒ Θ(n^(log_b a)) -
a = b^k ⇒ Θ(n^k log^(p+1) n) -
a < b^k ⇒ Θ(n^k log^p n)
| Recurrence | a, b, k | Result |
|---|---|---|
| T(n) = T(n/2) + 1 | 1, 2, 0 | Θ(log n) |
| T(n) = T(n/2) + n | 1, 2, 1 | Θ(n) |
| T(n) = 2T(n/2) + 1 | 2, 2, 0 | Θ(n) |
| T(n) = 2T(n/2) + n | 2, 2, 1 | Θ(n log n) |
| T(n) = 2T(n/2) + n log n | 2, 2, 1 (p = 1) | Θ(n log² n) |
| T(n) = 4T(n/2) + n | 4, 2, 1 | Θ(n²) |
| T(n) = 4T(n/2) + n² | 4, 2, 2 | Θ(n² log n) |
| T(n) = 3T(n/2) + n | 3, 2, 1 | Θ(n^1.585) |
| T(n) = 7T(n/2) + n² | 7, 2, 2 | Θ(n^2.807) |
| T(n) = 3T(n/4) + n log n | 3, 4, 1 (p = 1) | Θ(n log n) |
Worked example
Problem. Solve T(n) = 9T(n/3) + n.
Solution. a = 9, b = 3, k = 1; b^k = 3 < 9, so Case 1: T(n) = Θ(n^(log₃ 9)) = Θ(n²).
Recurrences the Master theorem cannot handle
- Linear reduction: T(n) = T(n−1) + c ⇒ Θ(n); T(n) = T(n−1) + n ⇒ Θ(n²); T(n) = T(n−1) + log n ⇒ Θ(n log n) (since Σ log i = log n!).
- Exponential branching: T(n) = 2T(n−1) + 1 with T(0) = 0 gives T(n) = 2^n − 1 = Θ(2^n) (Tower of Hanoi). Naive Fibonacci T(n) = T(n−1) + T(n−2) + 1 is Θ(φ^n), φ ≈ 1.618.
- Square-root recurrence: T(n) = T(√n) + 1 ⇒ Θ(log log n). Substitute n = 2^m, so T(2^m) = T(2^(m/2)) + 1, then S(m) = S(m/2) + 1 = Θ(log m).
- Unequal splits: T(n) = T(n/3) + T(2n/3) + n. Each tree level costs at most n and the depth is log_{3/2} n, so T(n) = Θ(n log n). Any split T(αn) + T((1−α)n) + n with constant α gives Θ(n log n).
- Method of expansion: unroll the recurrence k times, spot the pattern, and substitute the value of k that reaches the base case.
- Recursion tree: cost per level × number of levels. If cost per level is constant (equal), multiply by depth; if it grows geometrically, the last level dominates; if it shrinks geometrically, the root dominates.
- Substitution method: guess the bound and prove it by induction. To prove T(n) = 2T(n/2) + n is O(n log n) assume T(m) ≤ c·m log m for m < n; if the induction fails by a lower-order term, strengthen the guess to c·n log n − d·n.
- T(n) = T(n/2) + T(n/4) + n: the subproblem sizes add up to 3n/4 < n, so the cost per level shrinks geometrically and the root dominates: T(n) = Θ(n). Whenever the fractions sum to less than 1 with f(n) = n, the answer is Θ(n).
- T(n) = 2T(n/2) + n/log n falls in the gap between Case 1 and Case 2: level i costs n/(log n − i), so the total is n·(1 + 1/2 + … + 1/log n) = Θ(n log log n).
Formulas to remember
-
T(n) = T(n−1) + n ⇒ n(n+1)/2 = Θ(n²) -
T(n) = 2T(n−1) + 1, T(0) = 0 ⇒ 2^n − 1 -
T(n) = T(√n) + 1 ⇒ Θ(log log n)
Worked example
Problem. Solve T(n) = T(n/3) + T(2n/3) + n.
Solution. Each level sums to at most n; the longest path has depth log_{3/2} n. So T(n) = O(n log n), and also Ω(n log n) because the shallowest path has depth log₃ n, so T(n) = Θ(n log n).
Classic D&C algorithms at GATE depth
- Merge sort: T(n) = 2T(n/2) + n − 1 gives n log₂ n − n + 1 comparisons when n is a power of two (n = 16 ⇒ 49).
- Max and min by D&C: T(n) = 2T(n/2) + 2 with T(2) = 1 gives 3n/2 − 2 comparisons (n = 8 ⇒ 10), fewer than the 2n − 2 of the naive method.
- Strassen multiplies two n × n matrices using 7 recursive multiplications of n/2 matrices instead of 8: T(n) = 7T(n/2) + Θ(n²) = Θ(n^(log₂ 7)) ≈ Θ(n^2.807). The classical algorithm is Θ(n³).
- Karatsuba multiplies two n-digit integers with 3 half-size multiplications: T(n) = 3T(n/2) + Θ(n) = Θ(n^(log₂ 3)) ≈ Θ(n^1.585), against Θ(n²) for the schoolbook method.
- Maximum subarray by D&C: best of (left half, right half, crossing the midpoint) with a Θ(n) crossing scan, so T(n) = 2T(n/2) + n = Θ(n log n); Kadane DP does it in Θ(n).
- Closest pair of points: sort by x, split, solve each half, then check a strip of width 2δ in Θ(n) — overall Θ(n log n). Median of medians selects the k-th element in worst-case Θ(n).
- Median of medians recurrence: T(n) = T(n/5) + T(7n/10) + Θ(n). Since 1/5 + 7/10 = 9/10 < 1 the work shrinks geometrically, giving Θ(n). With groups of 3 the sizes n/3 + 2n/3 = n and the answer degrades to Θ(n log n), which is why groups of 5 are used.
- Fast exponentiation computes a^n with T(n) = T(n/2) + 1 = Θ(log n) multiplications; Fibonacci F(n) can be computed in Θ(log n) multiplications by raising the matrix [[1,1],[1,0]] to the power n.
- Counting inversions: modify merge sort so that each time an element of the right half is copied before elements of the left half, you add the number of remaining left elements. Time Θ(n log n) instead of the Θ(n²) brute force.
- Binary search on n elements needs at most ⌊log₂ n⌋ + 1 comparisons: n = 1023 gives 10, n = 1024 gives 11. Merge sort on n = 8 uses at most 17 comparisons and at least 12.
Worked example
Problem. How many comparisons does the D&C max-min algorithm use on n = 16 elements?
Solution. 3n/2 − 2 = 24 − 2 = 22.
Worked trace — merge sort on 7 elements
- Input [38, 27, 43, 3, 9, 82, 10], n = 7. Divide: [38 27 43 3] | [9 82 10]; again: [38 27] [43 3] [9 82] [10]; again: single elements. Depth = ⌈log₂ 7⌉ = 3.
- Combine bottom-up: [27 38], [3 43], [9 82], [10]; then [3 27 38 43] and [9 10 82]; finally [3 9 10 27 38 43 82].
- Merging runs of sizes p and q takes at most p + q − 1 comparisons (worst case) and at least min(p, q) (best case, one run entirely smaller than the other).
- Worst case for [27 38] with [3 43]: compare 27–3, 27–43, 38–43 = 3 = 2 + 2 − 1 comparisons. Best case for [3 27] with [38 43]: only 2 comparisons.
- The recursion makes 2n − 1 = 13 calls in total for n = 7 (n leaves + n − 1 internal calls); this count also equals the number of nodes of the recursion tree.
- Exact recurrence for the worst case: T(n) = 2T(n/2) + n − 1, T(1) = 0, giving n log₂ n − n + 1 for n a power of two.
Worked example
Problem. Find the worst-case number of comparisons of merge sort for n = 8.
Solution. T(1) = 0; T(2) = 2·0 + 1 = 1; T(4) = 2·1 + 3 = 5; T(8) = 2·5 + 7 = 17. Check: 8·3 − 8 + 1 = 17.
GATE exam tricks — recurrences quickly
- Write a, b and f(n) first. Compute n^(log_b a) (like n² for 4T(n/2)) and compare it with f(n); the larger polynomial wins, equal gives an extra log.
- Memorise the base cases: a = b and f = n gives n log n; a = 1, f = 1 gives log n; a = 2, b = 2, f = 1 gives n; a = 4, b = 2 gives n² (before f matters).
- If f(n) = n^k log^p n and a = b^k then the answer is n^k log^(p+1) n — a favourite GATE trap (n log n against 2T(n/2) gives n log² n).
- Check applicability: a must be a constant, not n or 2^n; subproblems must be equal size; otherwise use the tree or substitution.
- For T(n) = T(n−1) + f(n) the answer is Σ f(i). For T(n) = c·T(n−1) + f(n) with c > 1 the answer is exponential Θ(c^n).
- For T(n) = T(√n) + c, use n = 2^m. For T(n) = 2T(√n) + log n, set n = 2^m to get S(m) = 2S(m/2) + m = Θ(m log m), i.e., Θ(log n · log log n).
- Exact comparison counts (merge sort 49 for n = 16, max–min 22 for n = 16) follow from the exact recurrence with the stated base case — never from Θ.
- Worked example 2: T(n) = 4T(n/2) + n² log n. Here a = 4, b = 2, k = 2, p = 1 and a = b^k, so T(n) = Θ(n² log² n). The log factor of f(n) always becomes one more log in Case 2.
- Worked example 3 (elimination): T(n) = 2T(n/2) + n² must be Θ(n²), because n² beats n^(log₂ 2) = n; any option containing n² log n is a trap, since that needs a = b^k.
- Sanity check by recursion tree: if the root cost f(n) is ≥ the sum of the next level (Case 3), the answer is f(n); if the leaf count n^(log_b a) exceeds f(n), the answer is the leaf count.
Worked example
Problem. Solve T(n) = 2T(n/2) + n log n.
Solution. a = 2 = b^1 with k = 1 and p = 1, so T(n) = Θ(n log^(1+1) n) = Θ(n log² n).
Exam tips
- Identify a, b and f(n) in the first five seconds and compare n^(log_b a) with f(n).
- Remember the log-factor extension: equal growth with f = n^k log^p n gives n^k log^(p+1) n.
- For T(n−1)-type recurrences use expansion; the Master theorem will not apply.
- Memorise exact counts: merge sort ≈ n log n − n + 1, max–min 3n/2 − 2, binary search ⌊log n⌋ + 1.
- Strassen 7 multiplications ⇒ n^2.81; Karatsuba 3 multiplications ⇒ n^1.585.
10 GATE-style MCQs for Divide & Conquer with full explanations. Attempt each question before revealing the answer.
The solution of T(n) = 2T(n/2) + n, with T(1) = 1, is:
Show answer & explanation
Answer: B
a = 2, b = 2, k = 1: a = b^k, so Case 2 gives Θ(n log n).
The solution of T(n) = 4T(n/2) + n is:
Show answer & explanation
Answer: C
a = 4 > b^k = 2, so T(n) = Θ(n^(log₂ 4)) = Θ(n²).
The solution of T(n) = 3T(n/4) + n log n is:
Show answer & explanation
Answer: A
n^(log₄ 3) ≈ n^0.79 is polynomially smaller than f(n) = n log n, and the regularity condition holds (3·(n/4)·log(n/4) ≤ (3/4)·n log n), so Case 3 gives Θ(n log n).
The solution of T(n) = T(n − 1) + n, T(1) = 1, is:
Show answer & explanation
Answer: C
Expanding gives n + (n−1) + … + 1 = n(n+1)/2 = Θ(n²).
For T(n) = 2T(n − 1) + 1 with T(0) = 0, T(n) equals:
Show answer & explanation
Answer: C
T(1) = 1, T(2) = 3, T(3) = 7, … so T(n) = 2^n − 1 = Θ(2^n) (the Tower of Hanoi count).
The solution of T(n) = T(√n) + 1 is:
Show answer & explanation
Answer: B
Let n = 2^m. Then S(m) = S(m/2) + 1 = Θ(log m) = Θ(log log n).
Strassen algorithm multiplies two n × n matrices with the recurrence T(n) = 7T(n/2) + Θ(n²). The complexity is:
Show answer & explanation
Answer: B
a = 7 > b^k = 4, so T(n) = Θ(n^(log₂ 7)) ≈ Θ(n^2.807).
The divide-and-conquer method of finding the max and min of n = 8 elements (T(n) = 2T(n/2) + 2, T(2) = 1) uses how many comparisons?
Show answer & explanation
Answer: B
T(2) = 1, T(4) = 2·1 + 2 = 4, T(8) = 2·4 + 2 = 10, which equals 3n/2 − 2 for n = 8.
The solution of T(n) = T(n/3) + T(2n/3) + n is:
Show answer & explanation
Answer: B
Each level costs at most n and the tree depth is between log₃ n and log_{1.5} n, so T(n) = Θ(n log n).
The Master theorem CANNOT be applied directly to which recurrence?
Show answer & explanation
Answer: B
The number of subproblems a must be a constant; here a = 2^n depends on n. The other three are standard Master theorem forms.
Extra page-specific practice beyond the GATE samples. Attempt all 10, then reveal answers to check your reasoning.
The solution of T(n) = T(n/2) + 1 (binary search) is:
Show answer & explanation
Answer: B) Θ(log n)
a = 1 = b^0 (k = 0), so Case 2 gives Θ(n^0 log n) = Θ(log n).
The solution of T(n) = T(n/2) + n is:
Show answer & explanation
Answer: B) Θ(n)
a = 1 < b^k = 2, so Case 3 gives Θ(n). Indeed n + n/2 + n/4 + … = 2n.
The solution of T(n) = 2T(n/2) + 1 is:
Show answer & explanation
Answer: B) Θ(n)
a = 2 > b^0 = 1, so T(n) = Θ(n^(log₂ 2)) = Θ(n).
The solution of T(n) = 9T(n/3) + n is:
Show answer & explanation
Answer: C) Θ(n²)
a = 9 > 3^1 = 3, so T(n) = Θ(n^(log₃ 9)) = Θ(n²).
The solution of T(n) = 2T(n/2) + n² is:
Show answer & explanation
Answer: C) Θ(n²)
a = 2 < b^k = 4, so Case 3 gives Θ(n²).
The solution of T(n) = 8T(n/2) + n² is:
Show answer & explanation
Answer: C) Θ(n³)
a = 8 > b^k = 4, so T(n) = Θ(n^(log₂ 8)) = Θ(n³).
Karatsuba integer multiplication has time complexity:
Show answer & explanation
Answer: B) Θ(n^(log₂ 3))
T(n) = 3T(n/2) + Θ(n) gives Θ(n^(log₂ 3)) ≈ Θ(n^1.585).
The solution of T(n) = 2T(n/2) + n log n is:
Show answer & explanation
Answer: B) Θ(n log² n)
a = b^k with k = 1 and p = 1, so Θ(n log^(p+1) n) = Θ(n log² n).
The solution of T(n) = T(n − 1) + log n, T(1) = 0, is:
Show answer & explanation
Answer: C) Θ(n log n)
T(n) = Σ_{i=2}^{n} log i = log(n!) = Θ(n log n).
Merge sort on n = 16 elements performs at most how many comparisons (using M(n) = 2M(n/2) + n − 1, M(1) = 0)?
Show answer & explanation
Answer: B) 49
M(2) = 1, M(4) = 2 + 3 = 5, M(8) = 10 + 7 = 17, M(16) = 34 + 15 = 49 = 16·4 − 16 + 1.
Topic FAQs
Can I always use the Master theorem on T(n) = aT(n/b) + f(n)?
Only when a ≥ 1 is a constant, b > 1, subproblems have equal size and f(n) is polynomially comparable to n^(log_b a); otherwise use a recursion tree or substitution.
Why is merge sort Θ(n log n) but T(n) = 2T(n/2) + n log n is Θ(n log² n)?
Merge sort combines in Θ(n), which equals n^(log₂ 2); the extra log factor in f(n) adds one more log factor to the result.
How is D&C different from dynamic programming?
D&C solves independent subproblems recursively, whereas DP stores answers because subproblems overlap and are reused.
What is the space complexity of recursive merge sort?
Θ(n) for the auxiliary merge array plus Θ(log n) recursion stack, i.e., Θ(n) overall.
Hover a card to reveal the answer with a 3D flip. On touch devices, tap the card or use Show answer.