in Algorithms
368 views
0 votes
0 votes

hope(n){
if (n == 1)
G(n)
else
F() + hope(n/2);
}
What is the time complexity for the given function, if the function ‘G’ and function ‘F’ take O(1) and O(n) unit of time respectively.
My views: If we take the recursion in the form of "T(n) = T(n/2) + O(n)" then the answer is definitely O(n). But my understanding is still incomplete. If we don't consider that form mentioned above and imagine hope(n/2) being  recursively called, isn't F() being called 'log n'  times as well? Why can't the answer be O(nlogn) since F() (whose complexity is O(n)) is called 'log n' times?

in Algorithms
368 views

2 Comments

@Warlock lord

yes F() is called logn times, but complexity of F() will not be O(n) in every call, it will also decrease with n.

it will go like-> n + n/2 + n/4 + n/8.........logn times = O(n)

what you are doing is -> n + n + n + n...........logn times = O(nlogn)  // this is incorrect

0
0
yes understood :) thanks nitish
0
0

Please log in or register to answer this question.

Related questions