G27
Self-checkedTier 1 11 min readalgo.recurrence-solving

Solving recurrences: substitution, tree and Master theorem

Solving the recurrences of recursive algorithms: the recursion tree, the substitution method with induction, the Master theorem with its three cases and the logarithmic extension, the recurrences it does not cover, and a table of the standard solutions.

Why it exists

A recursive algorithm's cost satisfies an equation in terms of itself: mergesort on nn items sorts two halves and then merges, so T(n)=2T(n/2)+Θ(n)T(n) = 2T(n/2) + \Theta(n). To compare algorithms we need T(n)T(n) in closed form, at least its order. Three tools do almost everything: the recursion tree (see the pattern), the Master theorem (a shortcut for the most common shape), and substitution (proof by induction of a guess).

Prerequisites

Asymptotic notation
Recurrence relationsplanned

You need asymptotic notation (asymptotic notation) and a little about recurrence relations (recurrence relations).

Precise definitions

Recurrence. An equation defining T(n)T(n) by smaller values of TT together with a base case (usually T(1)=Θ(1)T(1) = \Theta(1), which we assume whenever it is not written). Floors and ceilings in the arguments (n/2 as ⌊n/2⌋\lfloor n/2 \rfloor) do not change the order for the recurrences here, so one pretends that nn is a power of the divisor.

Divide-and-conquer recurrence. T(n)=a T(n/b)+f(n)T(n) = a\,T(n/b) + f(n) with a≥1a \ge 1 subproblems of size n/bn/b (b>1b > 1) and f(n)f(n) the cost of dividing and combining.

Recursion tree method. Draw the tree of calls. Level ii has aia^i subproblems of size n/bin/b^i; if f(n)=nkf(n) = n^k, one subproblem at level ii costs (n/bi)k(n/b^i)^k and the whole level costs ai(n/bi)k=nk(a/bk)ia^i (n/b^i)^k = n^k (a / b^k)^i. There are log⁡bn+1\log_b n + 1 levels, the last one with alog⁡bn=nlog⁡baa^{\log_b n} = n^{\log_b a} leaves. The total is the sum over the levels, a geometric series with ratio a/bka/b^k:

  • ratio >1> 1 (the levels get more expensive downwards): dominated by the leaves, Θ(nlog⁡ba)\Theta(n^{\log_b a});
  • ratio =1= 1 (every level costs the same, nkn^k): Θ(nklog⁡n)\Theta(n^k \log n);
  • ratio <1< 1 (the levels get cheaper): dominated by the root, Θ(nk)=Θ(f(n))\Theta(n^k) = \Theta(f(n)).

The Master theorem. Let T(n)=a T(n/b)+f(n)T(n) = a\,T(n/b) + f(n) with constants a≥1a \ge 1, b>1b > 1, and let c=log⁡bac = \log_b a (so ncn^c is the number of leaves). Then

  1. If f(n)=O(nc−ε)f(n) = O(n^{c - \varepsilon}) for some ε>0\varepsilon > 0 (ff polynomially smaller): T(n)=Θ(nc)T(n) = \Theta(n^{c}).
  2. If f(n)=Θ(nclog⁡pn)f(n) = \Theta(n^{c} \log^{p} n) with p≥0p \ge 0 (ff the same order, up to logarithms): T(n)=Θ(nclog⁡p+1n)T(n) = \Theta(n^{c} \log^{p+1} n). For p=0p = 0: Θ(nclog⁡n)\Theta(n^c \log n).
  3. If f(n)=Ω(nc+ε)f(n) = \Omega(n^{c + \varepsilon}) for some ε>0\varepsilon > 0 (ff polynomially larger) and the regularity condition af(n/b)≤δf(n)a f(n/b) \le \delta f(n) holds for some constant δ<1\delta < 1 and large nn: T(n)=Θ(f(n))T(n) = \Theta(f(n)).

For f(n)=nklog⁡pnf(n) = n^k \log^p n the conditions reduce to comparing kk with c=log⁡bac = \log_b a: k<ck < c is case 1; k=ck = c is case 2; k>ck > c is case 3 (the regularity condition holds automatically for such ff). The theorem does not cover the gap where ff is smaller than ncn^c only by a logarithmic factor: for f(n)=nc/log⁡nf(n) = n^c / \log n the solution is Θ(nclog⁡log⁡n)\Theta(n^c \log \log n), and for f(n)=nclog⁡pnf(n) = n^c \log^{p} n with p<−1p < -1 it is Θ(nc)\Theta(n^c) (these need the tree).

Substitution method. (1) Guess the form of the solution (from the tree, or from small cases). (2) Prove by induction that T(n)≤c g(n)T(n) \le c\, g(n) for a suitable constant cc, assuming it for smaller arguments, substituting into the recurrence. (3) Check the base case. If the induction fails by a lower-order term, strengthen the hypothesis by subtracting a lower-order term.

Change of variable. For T(n)=T(n)+1T(n) = T(\sqrt n) + 1 put n=2mn = 2^m: S(m)=T(2m)S(m) = T(2^m) satisfies S(m)=S(m/2)+1S(m) = S(m/2) + 1, so S(m)=Θ(log⁡m)S(m) = \Theta(\log m) and T(n)=Θ(log⁡log⁡n)T(n) = \Theta(\log \log n).

Recurrences with subtraction. T(n)=T(n−1)+f(n)T(n) = T(n-1) + f(n) is solved by summing: T(n)=T(0)+∑i=1nf(i)T(n) = T(0) + \sum_{i=1}^{n} f(i) (so f=nf = n gives n(n+1)/2n(n+1)/2, f=1f = 1 gives nn). With a multiplier, T(n)=2T(n−1)+1T(n) = 2T(n-1) + 1 doubles at each step: T(n)=2n−1T(n) = 2^n - 1, exponential. T(n)=T(n−1)+T(n−2)T(n) = T(n-1) + T(n-2) grows like the Fibonacci numbers, Θ(φn)\Theta(\varphi^n) with φ=(1+5)/2≈1.618\varphi = (1 + \sqrt 5)/2 \approx 1.618. The Master theorem does not apply to any of these (the subproblem size is n−1n - 1, not n/bn/b).

How it works

The recursion tree of T(n)=2T(n/2)+nT(n) = 2T(n/2) + n for n=1024n = 1024. Every level costs 1024: two subproblems of half the size replace one, and the sum of sizes is constant. There are 11 levels, so the total is about nlog⁡2nn \log_2 n. This is the pattern of mergesort.

Recursion tree of T(n) = 2T(n/2) + n: start

Computed by a script, not typed
levelsubproblemssize of eachcost of onecost of the level
0
1
2
3
4
5
6
7
8
9
10

Step 0

Every level of the tree of T(n) = 2T(n/2) + n costs the same, n: the number of subproblems doubles while their size halves. Here n = 1024, so there are 11 levels.

0 / 12
computed by script: the quantities are evaluated or measured, one row at a time. python3 scripts/gen-algo-traces.py

For T(n)=T(n/2)+nT(n) = T(n/2) + n the levels cost 1024,512,256,…1024, 512, 256, \ldots: a geometric series with ratio 1/21/2, dominated by the root. The total is about 2n2n: T(n)=Θ(n)T(n) = \Theta(n) (case 3).

Recursion tree of T(n) = T(n/2) + n: start

Computed by a script, not typed
levelsubproblemssize of eachcost of onecost of the level
0
1
2
3
4
5
6
7
8
9
10

Step 0

Now there is one subproblem per level, half the size: the costs per level fall geometrically.

0 / 12
computed by script: the quantities are evaluated or measured, one row at a time. python3 scripts/gen-algo-traces.py

For T(n)=4T(n/2)+nT(n) = 4T(n/2) + n the levels cost n,2n,4n,…n, 2n, 4n, \ldots: ratio 2, dominated by the leaves, nlog⁡24=n2n^{\log_2 4} = n^2 of them: T(n)=Θ(n2)T(n) = \Theta(n^2) (case 1).

Recursion tree of T(n) = 4T(n/2) + n: start

Computed by a script, not typed
levelsubproblemssize of eachcost of onecost of the level
0
1
2
3
4
5
6
7
8
9
10

Step 0

Four subproblems of half the size: the number of subproblems grows faster than their cost shrinks.

0 / 12
computed by script: the quantities are evaluated or measured, one row at a time. python3 scripts/gen-algo-traces.py

Ten Master theorem verdicts for standard algorithms. For each, T(bm)T(b^m) was evaluated exactly for mm up to 60 and the claimed bound was confirmed (the ratio T(n)/g(n)T(n)/g(n) stays steady, so it is neither too low nor too high by a factor such as log⁡n\log n).

Master theorem verdicts: start

Computed by a script, not typed
recurrencen^(log_b a)f(n)casesolutionconfirmed
1
2
3
4
5
6
7
8
9
10

Step 0

Master theorem for T(n) = aT(n/b) + f(n): compare f(n) with n^(log_b a). If f is polynomially smaller the leaves dominate (case 1); if f has the same growth, every level costs about the same and a log factor appears (case 2); if f is polynomially larger the root dominates (case 3). Each verdict below was confirmed by evaluating T(b^m) for m up to 60 and checking that T(n) / g(n) stays within a factor of two between m = 20 and m = 60.

0 / 10
computed by script: the quantities are evaluated or measured, one row at a time. python3 scripts/gen-algo-traces.py

And the recurrences that need other tools:

Recurrences outside the Master theorem: start

Computed by a script, not typed
recurrencesolutionwhymethod
1
2
3
4
5
6

Step 0

Recurrences that are not of the form aT(n/b) + f(n), or that need another method. Each solution below was confirmed numerically by evaluating the recurrence.

0 / 6
computed by script: the quantities are evaluated or measured, one row at a time. python3 scripts/gen-algo-traces.py

Worked examples

EX 1Master theorem: read off a, b, f

Easy

T(n)=3T(n/2)+nT(n) = 3T(n/2) + n (Karatsuba): a=3a = 3, b=2b = 2, c=log⁡23≈1.585c = \log_2 3 \approx 1.585, f(n)=n=n1f(n) = n = n^1, 1<1.5851 < 1.585: case 1, T(n)=Θ(nlog⁡23)≈Θ(n1.585)T(n) = \Theta(n^{\log_2 3}) \approx \Theta(n^{1.585}). T(n)=2T(n/2)+n2T(n) = 2T(n/2) + n^2: c=1<2c = 1 < 2: case 3 (regularity: 2(n/2)2=n2/2≤12n22 (n/2)^2 = n^2/2 \le \tfrac12 n^2), Θ(n2)\Theta(n^2). T(n)=T(n/2)+1T(n) = T(n/2) + 1 (binary search): a=1a = 1, c=0c = 0, f=n0f = n^0: case 2, Θ(log⁡n)\Theta(\log n). T(n)=8T(n/2)+n2T(n) = 8T(n/2) + n^2: c=3>2c = 3 > 2: Θ(n3)\Theta(n^3). T(n)=7T(n/2)+n2T(n) = 7T(n/2) + n^2 (Strassen): c=log⁡27≈2.807>2c = \log_2 7 \approx 2.807 > 2: Θ(n2.807)\Theta(n^{2.807}).

EX 2A logarithm in f(n)

Easy

T(n)=2T(n/2)+nlog⁡nT(n) = 2T(n/2) + n \log n: c=1c = 1 and f=nlog⁡n=n1log⁡1nf = n \log n = n^1 \log^1 n, the middle case with p=1p = 1: T(n)=Θ(nlog⁡2n)T(n) = \Theta(n \log^2 n) (a tree with log⁡n\log n levels each costing about nlog⁡nn \log n at the top, with the logarithm shrinking towards the bottom, gives log⁡2n/2\log^2 n / 2 in the sum). By contrast T(n)=3T(n/4)+nlog⁡nT(n) = 3T(n/4) + n \log n: c=log⁡43≈0.79<1c = \log_4 3 \approx 0.79 < 1, so ff is polynomially larger, case 3, with regularity 3(n/4)log⁡(n/4)≤34nlog⁡n3 (n/4) \log(n/4) \le \tfrac34 n \log n: T(n)=Θ(nlog⁡n)T(n) = \Theta(n \log n). Case 3 needs ff to be larger by a polynomial factor: nlog⁡nn \log n versus n0.79n^{0.79} qualifies.

EX 3Substitution proof

Medium

Claim: T(n)=2T(⌊n/2⌋)+nT(n) = 2T(\lfloor n/2 \rfloor) + n is O(nlog⁡n)O(n \log n). Hypothesis: T(m)≤c mlog⁡2mT(m) \le c\,m \log_2 m for all 2≤m<n2 \le m < n. Then T(n)≤2c⌊n/2⌋log⁡2⌊n/2⌋+n≤c nlog⁡2(n/2)+n=c nlog⁡2n−c n+n≤c nlog⁡2nT(n) \le 2c \lfloor n/2 \rfloor \log_2 \lfloor n/2 \rfloor + n \le c\,n \log_2 (n/2) + n = c\,n \log_2 n - c\,n + n \le c\,n \log_2 n for c≥1c \ge 1. The base cases n=2,3n = 2, 3 are covered by choosing cc large enough. A subtle case: T(n)=2T(n/2)+1T(n) = 2T(n/2) + 1 is O(n)O(n), but the induction T(n)≤cnT(n) \le c n gives T(n)≤cn+1T(n) \le c n + 1, which does not close; the stronger hypothesis T(n)≤cn−1T(n) \le c n - 1 gives 2(cn/2−1)+1=cn−12(c n/2 - 1) + 1 = c n - 1 and works. (The exact solution with T(1)=1T(1) = 1 is T(n)=2n−1T(n) = 2n - 1; checked for n=2mn = 2^m up to m=39m = 39.)

EX 4When the Master theorem does not apply

Medium

T(n)=2T(n/2)+n/log⁡nT(n) = 2T(n/2) + n / \log n: c=1c = 1 and ff is smaller than nn but not by a polynomial factor, so no case applies. Level ii of the tree (with n=2mn = 2^m) costs 2i⋅n/2ilog⁡2(n/2i)=nm−i2^i \cdot \dfrac{n/2^i}{\log_2 (n/2^i)} = \dfrac{n}{m - i}, so the sum over the levels is n(1+12+13+⋯+1m)n(1 + \tfrac12 + \tfrac13 + \cdots + \tfrac1m) plus the leaves: T(2m)=2m(1+Hm)T(2^m) = 2^m(1 + H_m), where Hm=1+12+⋯+1mH_m = 1 + \tfrac12 + \cdots + \tfrac1m is the harmonic sum: T(n)=Θ(nlog⁡log⁡n)T(n) = \Theta(n \log \log n). (Confirmed with the exact value for mm up to 39.) T(n)=2T(n−1)+1T(n) = 2T(n-1) + 1: subtraction, not division: T(n)=2n−1T(n) = 2^n - 1. T(n)=T(n/3)+T(2n/3)+nT(n) = T(n/3) + T(2n/3) + n has two different subproblem sizes: every level of the tree costs at most nn and the depth is between log⁡3n\log_3 n and log⁡3/2n\log_{3/2} n, so T(n)=Θ(nlog⁡n)T(n) = \Theta(n \log n) (the numerical ratio T(n)/(nlog⁡2n)T(n) / (n \log_2 n) is steady for nn from 10310^3 to 10610^6).

EX 5Quick solutions

GATE level

(a) T(n)=T(n−1)+nT(n) = T(n-1) + n: Θ(n2)\Theta(n^2). (b) T(n)=T(n)+1T(n) = T(\sqrt n) + 1: Θ(log⁡log⁡n)\Theta(\log \log n). (c) T(n)=2T(n/2)+log⁡nT(n) = 2T(n/2) + \log n: c=1c = 1, f=log⁡n=O(n1−ε)f = \log n = O(n^{1-\varepsilon}): case 1, Θ(n)\Theta(n). (d) T(n)=4T(n/2)+n2log⁡nT(n) = 4T(n/2) + n^2 \log n: c=2c = 2, f=n2log⁡nf = n^2 \log n: middle case with p=1p = 1: Θ(n2log⁡2n)\Theta(n^2 \log^2 n). (e) T(n)=T(n/2)+T(n/4)+nT(n) = T(n/2) + T(n/4) + n: the two subproblems cost n/2n/2 and n/4n/4, so the levels shrink by 3/43/4: the root dominates, Θ(n)\Theta(n). (f) T(n)=16T(n/4)+nT(n) = 16T(n/4) + n: c=2c = 2, Θ(n2)\Theta(n^2). When a question asks "which of the following is the solution", try the Master theorem first, then the tree.

Cheat box

Cheat box

  • T(n)=aT(n/b)+f(n)T(n) = aT(n/b) + f(n): compare f(n)f(n) with nlog⁡ban^{\log_b a}.
  • Case 1: ff polynomially smaller gives Θ(nlog⁡ba)\Theta(n^{\log_b a}). Case 2: same order gives Θ(nlog⁡balog⁡n)\Theta(n^{\log_b a} \log n) (with log⁡p\log^p in ff: log⁡p+1\log^{p+1}). Case 3: ff polynomially larger (and regular) gives Θ(f)\Theta(f).
  • Standard: T(n/2)+1T(n/2) + 1: log⁡n\log n; 2T(n/2)+n2T(n/2) + n: nlog⁡nn \log n; 2T(n/2)+12T(n/2) + 1: nn; 4T(n/2)+n4T(n/2) + n: n2n^2; 3T(n/2)+n3T(n/2) + n: n1.585n^{1.585}; 7T(n/2)+n27T(n/2) + n^2: n2.807n^{2.807}; T(n/2)+nT(n/2) + n: nn.
  • Gap (f=nc/log⁡nf = n^c / \log n): tree gives nclog⁡log⁡nn^c \log \log n. Not Master: T(n−1)T(n-1) forms, unequal splits.
  • T(n)=T(n−1)+f(n)T(n) = T(n-1) + f(n): sum of ff. T(n)=2T(n−1)+1=2n−1T(n) = 2T(n-1) + 1 = 2^n - 1. Fibonacci-like: φn\varphi^n.
  • T(n)=T(n)+1T(n) = T(\sqrt n) + 1: log⁡log⁡n\log \log n. Unequal split T(n/3)+T(2n/3)+nT(n/3) + T(2n/3) + n: nlog⁡nn \log n.
  • Substitution: guess, induct, strengthen the hypothesis by subtracting a lower-order term if needed.

Traps and GATE patterns

Connections

Recurrences describe the cost of divide and conquer algorithms (mergesort, quicksort, binary search) and, with sums, of recursive algorithms generally. The tools of asymptotic notation are used throughout; analysing loops covers the iterative case. The mathematical side of linear recurrences is in recurrence relations.

PYQs

20 previous year questions from your book. I re-solved each one myself (19 by running or simulating, 1 by reasoning only); the book's key is shown next to my answer. All PYQs

GATE 2026 · Set 1 · MCQQ1 · page 97 of the book
Consider the following recurrence relations:
For all n>1,
T1(n)=4T1(n/2)+T2(n)
T2(n)=5T2(n/4)+Θ(log2n)
Assume that for all n≤1, T1(n)=1 and T2(n)=1.
Which one of the following options is correct?
  • (a)T1(n)=θ(n2)
  • (b)T1(n)=θ(n2log2n)
  • (c)T1(n)=θ(nlog4 5)
  • (d)T1(n) = Θ(nlog4⁡5log2⁡n)
GATE 2026 · Set 2 · MSQQ2 · page 97 of the book
Which of the following can be recurrence
relation(s) corresponding to an algorithm with time
complexity O(n)?
  • (a)T(n)=T(n-1) +1, T(1)=1
  • (b)T(n)=2T(n/2)+1 , T(1)=1
  • (c)T(n)=2T(n/2)+n , T(1)=1
  • (d)T(n)=T(n-1)+n , T(1)=1
GATE 2025 · Set 1 · MCQQ4 · page 97 of the book
Consider the following recurrence relation:
T(n)=2T(n-1)+ n2n for n>0, T(0)=1
Which ONE of the following options is CORRECT?
  • (a)T(n)=θ(n22n)
  • (b)T(n)=θ(n2n)
  • (c)T(n)=θ((logn)22n)
  • (d)T(n)=θ(4n) 4
GATE 2024 · Set 1 · MCQQ5 · page 97 of the book
Consider the following recurrence relation:

T(n) = {√n ⋅ T(√n) + n
for n ≥ 1
1
for n = 1
Which one of the following options is CORRECT?
  • (a)T(n) = θ(nloglogn)
  • (b)T(n) = θ(n2logn)
  • (c)T(n) = θ(nlogn)
  • (d)T(n) = θ(n2loglogn)
GATE 2024 · Set 2 · MCQ · 1 markQ6 · page 97 of the book
Let T(n) be the recurrence relation defined as
follows:
T(0) = 1,
T(1) = 2 and
T(n) = 5T(n - 1) - 6T(n - 2) for n ≥ 2
Which one of the following statement is TRUE?
  • (a)T(n) = θ(n·3n)
  • (b)T(n) = θ(n·2n)
  • (c)T(n) = θ(3n)
  • (d)T(n) = θ(2n) ALGORITHMS b b
GATE 2021 · Set 2 · MCQ · 2 marksQ10 · page 98 of the book
For constants a ≥ 1 and b > 1, consider the
following recurrence defined on the non-negative
integers:
T(n) = a T (n/b) + f(n)
Which one of the following options is correct about
the recurrence T(n) ?
  • (a)f(n) is O(nlog (a)-∈) for some ∈>0, then T(n) is T(n)=Θ(n log (a))
  • (b)If f(n) is n/log2(n), then T(n) is Θ(log2(n))
  • (c)If f(n) is Θ(nlogb (a)), then T(n) is Θ(nlogb (a))
  • (d)If f(n) is n log2(n), then T(n) is Θ(nlog2(n))
GATE 2021 · Set 1 · MCQ · 1 markQ12 · page 98 of the book
Consider the following recurrence relation.
T(n) = T(n/2) + T(2n/5) + 7n
, if n > 0
{
1 , if n = 0
GATE 2020 · MCQQ13 · page 99 of the book
For parameters a and b, both of which are ω(1),
T(n) = T(n 1/a ) + 1, and T(b)=1. Then T(n) is:
  • (a)θ(log ab n)
  • (b)θ(log a log b n)
  • (c)θ(log 2 log 2 n)
  • (d)θ(log b log a n)
GATE 2017 · Set 2 · MCQ · 2 marksQ16 · page 99 of the book
Consider the recurrence function
2 × T(√n) + 1,
if n > 2
GATE 2016 · Set 2 · NATQ17 · page 99 of the book
The given diagram shows the flowchart for a
recursive function A(n). Assume that all
statements, except for the recursive calls, have
O(1) time complexity. If the worst-case time
complexity of this function is O(nα ), then the least
possible value (accurate up to two decimal
positions) of α is
.
Flowchart for Recursive Function A(n)
GATE 2014 · Set 2 · MCQ · 1 markQ23 · page 100 of the book
Which one of the following correctly
determines the solution of the recurrence relation
with T(1)=1?
T(n)=2T(n/2) + logn
  • (a)θ( n )
  • (b)θ( n log n )
  • (c)θ( n2 )
  • (d)θ( log n )
GATE 2009 · MCQ · 1 markQ27 · page 101 of the book
The running time of an algorithm is represented
by the following recurrence relation:
n ; n ≤ 3
T(n) = {
n
T ( ) + cn; otherwise
3
GATE 2008 · MCQ · 2 marksQ29 · page 101 of the book
When n = 2(2k) for some k >= 0, the recurrence
relation T(n) = √(2)T(n/2) + √(n), T(1) = 1
evaluates to :
  • (a)(√n)(log n + 1)
  • (b)(√n) log n
  • (c)(√n) log (√n)
  • (d)n log (√n) if(n == 0 || n == 1) return n; else return (2*f1(n-1) + 3*f1(n-2)); } int f2(int n) { int i; int X[N], Y[N], Z[N] ; X[0] = Y[0] = Z[0] = 0; X[1] = 1; Y[1] = 2; Z[1] = 3; for(i = 2; i <= n; i++) { X[i] = Y[i-1] + Z[i-2]; Y[i] = 2*X[i]; Z[i] = 3*X[i]; } return X[n] ; } The running time of f1(n) and f2(n) are
  • (a)Θ(n) and Θ(n)
  • (b)Θ(2n) and Θ(n)
  • (c)Θ(n) and Θ(2n)
  • (d)Θ(2n) and Θ(2n) ALGORITHMS Check Out Our Gate Courses & Secure Your Dream Rank !
year not read yetQ35 · page 102 of the book
What is the Asymptotic Notation of the following
recursive function:
int DoSomething (int n) {
if (n <= 2)
return 1;
else
return (DoSomething (floor(sqrt(n))) + n); }
  • (a)θ (n2)
  • (b)θ (nlog2n)
  • (c)θ (log2n)
  • (d)θ (log2log2n)
GATE 2006 · MCQ · 1 markQ37 · page 102 of the book
Consider the following recurrence:

T(n) = 2T([√n]) + 1, T(1) = 1
Which one of the following is true?
int i,n;
for(i=2;i<=sqrt(n);i++)
if(n%i == 0)
{printf("Not Primen"); return 0;}
return 1;
} Let T(n)denote the number of times the for
loop is executed by the program on input n.
Which of the following is TRUE?
  • (a)T(n)=O(√n ) and T(n)=Ω(√n )
  • (b)T(n)=O(n) and T(n)=Ω(√n )
  • (c)T(n)=O(√n ) and T(n)=Ω(1)
  • (d)None of the above
  • (a)T(n)= θ (loglogn)
  • (b)T(n)= θ (logn)
  • (c)T(n)= θ (√n)
  • (d)T(n)= θ (n)
GATE 2005 · MCQQ38 · page 102 of the book
Suppose T(n)=2T(n/2)+n,
T(0)=T(1)=1
Which one of the following is FALSE?
  • (a)T(n)=O(n2)
  • (b)T(n)=Θ(n log n)
  • (c)T(n)=Ω(n2)
  • (d)T(n)=O(n log n) ALGORITHMS Check Out Our Gate Courses & Secure Your Dream Rank ! 4 4
GATE 2005 · MCQ · 1 markQ40 · page 103 of the book
Consider the following C-function:
double foo (int n)
{
int i;
double sum;
if (n = = 0) return 1.0;
else
{
sum = 0.0;
for (i = 0; i < n; i++)
sum += foo (i);
return sum;
}
}
Suppose we modify the above function foo() and
store the values of foo (i), 0⁡≤⁡i < n, as and
when they are computed. With this modification,
the time complexity for function foo() is
significantly reduced. The space complexity of
the modified function would be:
  • (a)O(1)
  • (b)O(n)
  • (c)O(n!)
  • (d)O(nn)
GATE 2008 · MCQ · 1 markQ37 · page 25 of the book
Consider the following C functions:

int f1(int n)

{

if (n == 0 || n == 1)

return n;

else

return (2 * f1(n - 1) + 3 * f1(n - 2));

}

int f2(int n)

{

int i;

int X[N], Y[N], Z[N];

X[0] = Y[0] = Z[0] = 0;

X[1] = 1;

Y[1] = 2;

Z[1] = 3;

for (i = 2; i <= n; i++) {
        X[i] = Y[i - 1] + Z[i - 2];
        Y[i] = 2 * X[i];
        Z[i] = 3 * X[i];

}

return X[n];

}

The running time of f1(n) and f2(n) are
  • (a)Θ(n) and Θ(n)
  • (b)Θ(2n) and Θ(n)
  • (c)Θ(n) and Θ(2n)
  • (d)Θ(2n) and Θ(2n)
GATE 2008 · MCQ · 1 markQ38 · page 25 of the book
f1(8) and f2(8) return the values
  • (a)1661 and 1640
  • (b)59 and 59
  • (c)1640 and 1640
  • (d)1640 and 1661
GATE 2012 · MCQQ9 · page 62 of the book
The recurrence relation capturing the optimal
execution time of the Towers of Hanoi problem with
n discs is
  • (a)T(n) = 2T(n - 2) + 2
  • (b)T(n)= 2T(n - 1) + n
  • (c)T(n)= 2T(n/2) + 1
  • (d)T(n)= 2T(n - 1) + 1

Self-check

Question 1

Solve T(n)=2T(n/2)+n2T(n) = 2T(n/2) + n^2.

Show answer

c=1<2c = 1 < 2: case 3 with regularity 2(n/2)2=n2/2≤12n22 (n/2)^2 = n^2 / 2 \le \tfrac12 n^2: T(n)=Θ(n2)T(n) = \Theta(n^2).

Question 2

Solve T(n)=9T(n/3)+n2T(n) = 9T(n/3) + n^2.

Show answer

c=log⁡39=2=kc = \log_3 9 = 2 = k: case 2, T(n)=Θ(n2log⁡n)T(n) = \Theta(n^2 \log n).

Question 3

Solve T(n)=T(n−1)+log⁡nT(n) = T(n-1) + \log n.

Show answer

∑i=1nlog⁡i=log⁡n!=Θ(nlog⁡n)\sum_{i=1}^{n} \log i = \log n! = \Theta(n \log n).

Question 4

Why can the Master theorem not solve T(n)=2T(n/2)+nlog⁡nT(n) = 2T(n/2) + n \log n with case 3, and what is the answer?

Show answer

nlog⁡nn \log n is not polynomially larger than nlog⁡22=nn^{\log_2 2} = n. It falls under case 2 with a logarithmic factor: T(n)=Θ(nlog⁡2n)T(n) = \Theta(n \log^2 n).

Revision card

One-screen revision

  • c=log⁡bac = \log_b a; compare ff with ncn^c: smaller gives ncn^c; equal gives nclog⁡nn^c \log n; larger (regular) gives ff.
  • Tree: levels cost nk(a/bk)in^k (a / b^k)^i: geometric up (leaves), constant (log factor), down (root).
  • Not Master: T(n−1)T(n-1) forms (sum, 2n2^n, Fibonacci), T(n)T(\sqrt n) (log⁡log⁡n\log \log n), unequal splits (nlog⁡nn \log n for n/3n/3, 2n/32n/3).
  • Substitution: guess, induct, strengthen if the lower-order term breaks the induction.

References

  • ·Cormen, Leiserson, Rivest, Stein, Introduction to Algorithms (3rd edition), Chapter 4 Divide-and-Conquer (4.3 the substitution method, 4.4 the recursion-tree method, 4.5 the master method)
  • ·Kleinberg and Tardos, Algorithm Design, Chapter 5 Divide and Conquer (5.2 further recurrence relations)

Verification

The Master theorem as implemented in scripts/algo_lib.py was compared with the exact numerical solution of 486 recurrences T(n) = a T(n/b) + n^k log^p n (a up to 9, b in 2 to 4, six exponents including fractions, log powers 0 to 2): at n = b^300 the ratio T(n) / g(n) for the claimed bound g is steady between n = b^100 and b^300, and a missing or an extra logarithm is rejected (scripts/enum-checks/algo_analysis.py). The solutions of the recurrences outside the Master theorem were checked by exact integers (2T(n-1) + 1 = 2^n - 1), exact sums (2T(n/2) + n/log n = n(1 + H_m)) or ratio tests. The recursion-tree tables are computed for n = 1024. Textbook chapter references are from memory; the statement of case 2 with a logarithmic factor follows the common extended form. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (loop counts, recurrences, merge and quicksort counts and averages, hash probes and load-factor formulas, DP tables, MST and shortest-path runs, Floyd matrices); long C programs were not re-read line by line (they are compiled and compared by npm run verify:c, and their printed outputs were checked against the prose); tables and animations are machine output and were read, not re-run; No other defect found. Status raised to self-checked; verified needs a textbook or human check.

last verified: never