GATE CSE
First time here? Checkout the FAQ!
x
0 votes
53 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;
        }
    }

 

asked in Algorithms by Veteran (19.9k points)  
edited by | 53 views
O(logn)

1 Answer

+3 votes
Best answer
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 (8.6k points)  
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.
Top Users Feb 2017
  1. Arjun

    5224 Points

  2. Bikram

    4230 Points

  3. Habibkhan

    3748 Points

  4. Aboveallplayer

    2986 Points

  5. Debashish Deka

    2356 Points

  6. sriv_shubham

    2298 Points

  7. Smriti012

    2142 Points

  8. Arnabi

    2008 Points

  9. sh!va

    1654 Points

  10. mcjoshi

    1628 Points

Monthly Topper: Rs. 500 gift card

20,832 questions
25,989 answers
59,623 comments
22,046 users