GATE CSE
First time here? Checkout the FAQ!
x
0 votes
61 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 (20.3k points)  
edited by | 61 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.7k 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 Jun 2017
  1. Bikram

    3694 Points

  2. Hemant Parihar

    1484 Points

  3. junaid ahmad

    1432 Points

  4. Arnab Bhadra

    1398 Points

  5. Niraj Singh 2

    1311 Points

  6. Rupendra Choudhary

    1194 Points

  7. rahul sharma 5

    1114 Points

  8. Arjun

    930 Points

  9. srestha

    928 Points

  10. Debashish Deka

    896 Points

Monthly Topper: Rs. 500 gift card
Top Users 2017 Jun 19 - 25
  1. Bikram

    1950 Points

  2. Niraj Singh 2

    1306 Points

  3. junaid ahmad

    502 Points

  4. sudsho

    410 Points

  5. akankshadewangan24

    388 Points


23,354 questions
30,065 answers
67,363 comments
28,382 users