0 0 votes What is the time complexity of the following code snippet? Assume "statement" takes $O(1)$ time. int x=0; int A(n) { statement; if(n==1)return 1; else { x + = 4A(n/2)+n^{2} return (x); } } $A)\theta(n^{2}logn)$ $B)\theta(logn)$ $C)\theta(n^{2})$ $D)\theta(nlogn)$ Algorithms algorithms time-complexity + – Lakshman Bhaiya 2.5k views answer comment Share Follow Print See all 8 Comments 8 8 Comments reply Lakshman Bhaiya commented Nov 3, 2018 reply Follow flag i got $A)$,but it is not the answer 0 0 replyShare Shubhanshu commented Nov 3, 2018 reply Follow flag Answer is B. 0 0 replyShare Lakshman Bhaiya commented Nov 3, 2018 reply Follow flag @Shubhanshu Yes, can you explain something? 0 0 replyShare Shubhanshu commented Nov 3, 2018 reply Follow flag It's recurrence relation will be $ T(n) = T(n/2) + 1$. 4T(n/2) doesn't mean in its RR u need to multiply T(n/2) by 4. Actually T(n/2) is calculated once and then it got multiply by 4 and similarly time to calculate n^2 is not n^2 but it is $O(1)$. 0 0 replyShare Lakshman Bhaiya commented Nov 3, 2018 reply Follow flag Ohh these things i missed and got another answer thanks 0 0 replyShare Soumya Tiwari commented Nov 3, 2018 reply Follow flag Recursion relation is T(n)=T(n/2) +O(1) This will give O(logn) 1 1 replyShare Lakshman Bhaiya commented Nov 3, 2018 reply Follow flag yes thanks 0 0 replyShare Mayankprakash commented Nov 3, 2018 reply Follow flag @shubhanshu @ soumya Can you please give one example where we will write as 4 × T(n/2) + O(1)? So that I can understand the difference when to multiply recurrence relation and when it is a only constant. Thanks 1 1 replyShare Please log in or register to add a comment.
1 1 vote 4A(n/2) numerical multiplication takes O(1) time so recurrence relation A(n) = A(n/2) + O(1) + O(1) a = 1 , b = 2 , k = 0 bk = 20 = 1 a = b p = 0 > -1 A(n) = θ(n logba* logn) = θ(logn) Correct answer = B Raja Rawal answered Nov 3, 2018 Raja Rawal comment Share Follow See all 2 Comments 2 2 Comments reply kirtipurohit commented Jan 9, 2022 reply Follow flag But in usual scenarios, log is taken of aT(n/b) + f(n) 0 0 replyShare kirtipurohit commented Jan 9, 2022 reply Follow flag loga b. How did you know its constant 0 0 replyShare Please log in or register to add a comment.