• retagged by
2,037 views
4 4 votes
int fun(int n)
    {
    int s=0,i;
    if(n<=1) return 1;
    for(i=1; i*i<n; i++)
    s+=n;
    return fun(n/4)+fun(n/4)+s;
    }


 

what will be the time complexity, returning value and no. of recursive calls of the above-given code?

3 Answers

Best answer
3 3 votes

1.Recurrence relation  for time complexity

will be T(n)=2T(n/4)+Root(n)

Because complexity of the loop is root(n)

By using case 2 of master theorem we have T(n)=theta(root(n)logn).

2.Recurrence relation  for return value

T(n)=2T(n/4)+Root(n)*n

because the function return value 2 time fun(n/4)+root(n)*n

by solving using  case 3 of master theorem we have solution of recurrence is theta(root(n)*n)

3.Recurrence relation for no of calls

T(n)=2T(n/4)+1

by solving using  case 1 of master theorem we have solution of recurrence is theta(root(n))

Correct me if i am wrong somewhere.

• selected by
0 0 votes
  • Asymptotic Time complexity = $n^{0.5} \cdot \log_2 n$
  • Asymptotic value = $k.n^{1.5}$ where $k \approx 1.32$
Position:
Show:

Related questions

1 1 vote
1 answers 1 answer
2.5k
2.5k views
radha gogia asked Jul 21, 2015
2,534 views
I have already gone through the links of stackoverflow on this topic but still couldn't understand it clearly , so please explain the logic behind this .
0 0 votes
3 3 answers
4.1k
4.1k views
gshivam63 asked May 19, 2016
4,103 views
int f(int x){if(x<1) return 1;else return f(x-1) +g(x/2);}int g(int x){if(x<2) return 1;else return f(x-1) +g(x/2);}a. LogarithmicB. QuadraticC. LinearD. Exponential
1 1 vote
2 2 answers
511
511 views
Vishnu__ asked Feb 4
511 views
#include <stdio.h>int mystery(int n) { if (n == 0) { return 0; } else if (n % 2 == 0) { return mystery(n / 2); } else { return 1 + mystery(n...
0 0 votes
1 1 answer
1.3k
1.3k views
Manisha Jaishwal asked Aug 6, 2022
1,304 views
Consider the following recursive function which is used by dynamic programming. T(n) = { 0; if n<1 1; if n=1 T(n-1)+T(n-2)+1; if n>1}Assume ...