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 ?

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 ? Correct Answer Exponential

Explanation

f(n) = f(n-1) + g(n)           ----> 1
g(n) = f(n-1) + g(n/2)       ----> 2

Putting the value of g(n) in equation 1,
f(n) = 2×f(n-1) + g(n) + g(n/2)

So, we can derive the below equation,(by approximation)
f(n) > 2f(n-1)
=> f(n) > 2×2 × f(n-2) ---- because f(n) > 2×f(n-1), so, f(n-1) > 2×2×f(n-2).... so on
=> f(n) > (2n) × f(1) --- here \'^\' denotes the exponent.
So, f(n) > θ(2n)

So, exponential growth for f(x).

correct answer is option 2

Related Questions