GATE CSE
First time here? Checkout the FAQ!
x
0 votes
57 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 | 57 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 Mar 2017
  1. rude

    5246 Points

  2. sh!va

    3054 Points

  3. Rahul Jain25

    2920 Points

  4. Kapil

    2732 Points

  5. Debashish Deka

    2602 Points

  6. 2018

    1574 Points

  7. Vignesh Sekar

    1440 Points

  8. Bikram

    1432 Points

  9. Akriti sood

    1420 Points

  10. Sanjay Sharma

    1128 Points

Monthly Topper: Rs. 500 gift card

21,553 questions
26,902 answers
61,258 comments
23,269 users