ISRO Scientist/Engineer CSE 2018Algorithms › Time Complexity and Recurrences
ISRO Scientist/Engineer CSE 2018 PYQ: The running time of an algorithm is given by: $T(n) = T(n-1) + T(n-2) - T(n-3)$, if $n > 3$ = $n$, otherwise…
The running time of an algorithm is given by:
$T(n) = T(n-1) + T(n-2) - T(n-3)$, if $n > 3$
= $n$, otherwise
Then what should be the relation between $T(1), T(2), T(3)$, so that the order of the algorithm is constant?
- A$T(1) = T(2) = T(3)$Correct answer
- B$T(1) + T(3) = 2T(2)$
- C$T(1) - T(3) = T(2)$
- D$T(1) + T(2) = T(3)$
Answer
(A) $T(1) = T(2) = T(3)$
Approach
Take first differences: D(n) = T(n) - T(n-1). The recurrence gives D(n) = T(n-2) - T(n-3) = D(n-2), so the differences are period-2: they alternate between a = T(2)-T(1) and b = T(3)-T(2). Summing the differences shows T(n) grows linearly unless the net change per period vanishes, i.e. a + b = T(3) - T(1) = 0. Equivalently, the characteristic equation $r^3 - r^2 - r + 1 = (r-1)^2(r+1) = 0$ gives T(n) = A + Bn $+ C(-1)^n$ with B = (T(3)-T(1))/2, so constant order requires T(1) = T(3). Only option A guarantees this: if T(1)=T(2)=T(3)=c, every difference is zero and T(n)=c for all n, clearly Θ(1). Options B, C and D do not force T(3)=T(1) and generally yield a linear term. In particular option B (T(1)+T(3)=2T(2)) puts the three base values in arithmetic progression, giving equal differences a=b and hence T(n)=T(1)+(n-1)a, a linear growth — so B is NOT sufficient. Hence A is the unique correct choice.
Reviewed by ISRO IQ Editorial Team.