71 views

What is the time complexibilty of the following code?Assume "statement" takes O(1) time.

int x=0;
int A(n)
{
statement;
if (n==1)
{
return 1;
}
else
{ x  += 4 A(n/2) + n2;
return x;
}
}

edited | 71 views
O(logn)

Computing $n^2$ is an $O(1)$ task and multiplying by $4$ is again an $O(1)$ work.

So recurrence would be $T(n) = T(n/2) + 1$, similar to binary search.

$T(n) =O( logn)$
answered by Boss (9.1k points) 4 64 210
selected by
Can you please tell me complexity on what basis? Like in sorting we could find complexity on number of comparisons. On what basis you have found here? If it is number of operations then don't you think we should include n² ?
A(n) is a function that calls A(n/2) multiplies it with 4 and adds n^2 and this goes on forming a recurrence relation.