478 views
0 0 votes
int func(int n)

{

if(n <= 1)

return n;

else

return 3*func(n-3) - 3*func(n-2);

 }

The running time of the above function is

1 Answer

0 0 votes

 

This is a recursive function, as func() is called inside func().

So we will write recurrence relation for it.

Assume T(n) is time complexity of func(n)

then,

$T(n) =\begin{Bmatrix} C, if(n≤1)\\T(n-3)+T(n-2)+C, if(n>1) \end{Bmatrix}$ 

 

Where C is the constant time taken by each recursive call to perform a return or an addition or a multiplication operation.

 

Now, solve it using recursive tree method.

T(n)=O($2^{n/2}$)

Or

T(n)=$\Omega$($2^{n/3}$)

 

 

I hope you got it.

Thank you.

 

 

 

Position:
Show:

Related questions

0 0 votes
1 1 answer
316
316 views
Shubham Sharma 2 asked Nov 24, 2025
316 views
The maximum of $x e^{-x}$ is reached at $x^{*}=$ $\_\_\_\_\_\_$
0 0 votes
1 1 answer
365
365 views
Shubham Sharma 2 asked Nov 24, 2025
365 views
The limit of the function $f(x)=\frac{\sin (\pi x)}{x}$ as $x$ tends to $0$ is equal to $\_\_\_\_\_\_\_$
0 0 votes
0 0 answers
134
134 views
Shubham Sharma 2 asked Nov 24, 2025
134 views
The function $f: \mathbb{R} \rightarrow \mathbb{R}$ is defined as\[f(x):=\left\{\begin{array}{ll}x^{2} \sin (1 / x) & \text { if } x \neq 0 \\0 & \text { if } x=0\end{arr...
0 0 votes
1 1 answer
182
182 views
Shubham Sharma 2 asked Nov 24, 2025
182 views
The sum of the squares of the eigenvalues of the matrix\[A=\left[\begin{array}{lll}1 & 2 & 3 \\3 & 1 & 4 \\4 & 5 & 1\end{array}\right]\]is $\_\_\_\_\_\_\_\_$ .