Skip to content

ISRO Scientist/Engineer CSE 2018Algorithms › Time Complexity and Recurrences

ISRO Scientist/Engineer CSE 2018 PYQ: Consider the following C code segment int f(int x) { if(x<1) return 1; else return (f(x-1)+g(x)); } int…

Consider the following C code segment

int f(int x)
{
    if(x<1) return 1;
    else return (f(x-1)+g(x));
}
int g(int x)
{
    if(x<2) return 2;
    else return (f(x-1)+g(x/2));
}

Of the following, which best describes the growth of $f(x)$ as a function of $x$ ?

  1. A
    Linear
  2. B
    Exponential
    Correct answer
  3. C
    Quadratic
  4. D
    Cubic

Answer

(B) Exponential

Approach

We analyze the mutual recursion between f and g. Substituting g's definition into f's recurrence yields f(x) = 2f(x-1) + g(x/2). The dominant term is 2f(x-1), which grows exponentially. The lower-order terms do not change the exponential nature.

Reviewed by ISRO IQ Editorial Team.