Last Updated : 13 Mar, 2019

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?

(A) Linear
(B) Exponential
(C) Quadratic
(D) Cubic


Answer: (B)

Explanation: f(x)=f(x−1)+g(x)
=f(x−1)+f(x−1)+g(x/2)
=2.f(x−1)+f(x/2−1)+g(x/4)

For simplicity remove the second term in the expansion and then tries to get a lower bound for f(x).

f(x)>2.f(x−1)

=>f(x)>2.2.f(x−2)
=>f(x)>2xf(1)
=>f(x)>2x

So, option B is true, exponential growth for f(x).

Quiz of this Question


Share your thoughts in the comments