Asymptotic Analysis & Recurrences
![Question 74, GATE 2008: Consider these C functions, assuming valid array bounds and the intended unit-cost arithmetic model:
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];
}
What are the running times of f1(n) and f2(n), respectively?
(A) Θ(n),Θ(n)
(B) Θ(2^n),Θ(n)
(C) Θ(n),Θ(2^n)
(D) Θ(2^n),Θ(2^n)](../../assets/questions/CS-2008-Q74-1.webp)
![Question 74, GATE 2008: Consider these C functions, assuming valid array bounds and the intended unit-cost arithmetic model:
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];
}
What are the running times of f1(n) and f2(n), respectively?
(A) Θ(n),Θ(n)
(B) Θ(2^n),Θ(n)
(C) Θ(n),Θ(2^n)
(D) Θ(2^n),Θ(2^n)](../../assets/questions/CS-2008-Q74-2.webp)
Answer & solution
Answer: No exact option; B is the apparent intended choice (no official key was published)
1. The recursive function makes one call on n−1 and one on n−2; coefficients 2 and 3 multiply the returned values and do not multiply the number of calls. Thus T(n)=T(n−1)+T(n−2)+Θ(1), giving Θ(φn), where φ=(1+√5)/2.
2. The iterative f2 executes n−1 constant-time iterations, Θ(n). None of the printed options gives Θ(φn),Θ(n).
3. Option B prints Θ(2n),Θ(n), which supplies an exponential upper bound but falsely labels it tight Θ. This analysis assumes unit-cost bounded-size arithmetic and n within valid array bounds; unbounded n in actual C would also encounter overflow.