58. How do you solve subtract-and-conquer recurrences such as T(n) = 2T(n-1) + 1 or T(n) = T(n-1) + n?
MediumAlgorithm analysis
Technical
Show model answer
Unroll the recurrence. For T(n) = 2T(n-1) + 1 with T(0) = 0: T(n) = 2T(n-1) + 1 = 4T(n-2) + 2 + 1 = ... = 2^k T(n-k) + (2^k - 1). At k = n this is 2^n - 1, so T(n) = Θ(2^n). This is exactly the Tower of Hanoi move count.
For T(n) = T(n-1) + n the unrolling gives n + (n-1) + ... + 1 = n(n+1)/2 = Θ(n²), the pattern behind selection sort or a naive recursive scan that does linear work per level. A general rule of thumb: for T(n) = aT(n-b) + Θ(n^k), the result is Θ(n^(k+1)) when a = 1 and Θ(n^k · a^(n/b)) when a > 1; when a < 1 it is Θ(n^k).