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

 

asked in Algorithms by Veteran (21.4k points) 30 103 201
edited by | 71 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 (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.

Related questions

+3 votes
1 answer
1
0 votes
0 answers
2
asked ago in Algorithms by ashwina Active (1.1k points) 2 5 28 | 41 views


Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
Top Users Oct 2017
  1. Arjun

    23398 Points

  2. Bikram

    17078 Points

  3. Habibkhan

    8264 Points

  4. srestha

    6296 Points

  5. Debashish Deka

    5438 Points

  6. jothee

    4978 Points

  7. Sachin Mittal 1

    4772 Points

  8. joshi_nitish

    4348 Points

  9. sushmita

    3964 Points

  10. Rishi yadav

    3804 Points


Recent Badges

Notable Question KISHALAY DAS
Notable Question sh!va
Notable Question abhijeetbzu
Great Question jothee
Popular Question rahul sharma 5
Nice Question mohit kumar 5
Notable Question rishu_darkshadow
Nice Comment Pranay Datta 1
Copy Editor Shivansh Gupta
Nice Comment KULDEEP SINGH 2
27,324 questions
35,176 answers
84,108 comments
33,279 users