GATE CS 2008 · Asymptotic Analysis & Recurrences

MCQ · 2 marks · Problem PROG-DIVC-0127

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 times of f1(n) and f2(n), respectively, are
  1. A
    \(\Theta (n)\) and \(\Theta (n)\)
  2. B
    \(2^{\Theta(n)}\) and \(\Theta (n)\)
  3. C
    \(\Theta (n)\) and \(2^{\Theta(n)}\)
  4. D
    \(2^{\Theta(n)}\) and \(2^{\Theta(n)}\)

Solve it on GATE DA practice →More Searching & Sorting Algorithms problems

Show the worked solution\[\boxed{T_1(n)=T_1(n-1)+T_1(n-2)+c\ \Rightarrow\ T_1(n)=\Theta(\varphi^n)=2^{\Theta(n)}}\]f1 makes two recursive calls with no memoisation, so its running time grows like the Fibonacci numbers, \(\Theta(\varphi^n)\) with \(\varphi\approx1.618\): exponential, \(2^{\Theta(n)}\).f2 fills the arrays in one loop of \(n-1\) iterations with \(O(1)\) work each: \(\Theta(n)\).(The official options wrote \(\Theta(2^n)\) for the exponential time; strictly f1 is \(\Theta(\varphi^n)\), which is \(2^{\Theta(n)}\) but not \(\Theta(2^n)\).)Answer: B

GATE DA practice: past exam problems by chapter, with worked solutions · gatedaprep.com