GATE CS 2003 Q35 — Asymptotic Worst Case Time and Space Complexity
Explain with AI
Consider the following recurrence relation
T(1)=1
T(n+1)=T(n)+⌊n+1⌋ for all n≥1
The value of T(m2) for m≥1 is
View worked solutionShowHide
Answer: B
We can solve this recurrence relation by substituting small values of m to eliminate options, or by analytical summation.
Method 1: Substitution (Fastest)
Using the recurrence relation T(n+1)=T(n)+⌊n+1⌋ with T(1)=1:
T(2)=T(1)+⌊2⌋=1+1=2
T(3)=T(2)+⌊3⌋=2+1=3
T(4)=T(3)+⌊4⌋=3+2=5
T(9)=16 and T(16)=38
Now, let us evaluate the options for m=4 where T(m2)=T(16)=38:
Option A:64(21(4)−39)+4=34=38
Option B:64(4(16)−3(4)+5)=32(57)=38 (Correct)
Method 2: Analytical Derivation
Expanding the recurrence gives:
T(m2)=1+j=2∑m2⌊j⌋
For any integer k, the value of ⌊j⌋=k for j∈[k2,(k+1)2−1]. The number of such terms is (k+1)2−k2=2k+1. Summing over all intervals from k=1 to m−1, and adding the final term for j=m2 (which is m):