I was solving a time-complexity question on Interview Bit, which is given below in the image.
The correct answer to this question is O(N). But according to me, the answe
Do the actual math:
T(N) = N + N/2 + N/4 + ... + 1 (log_2 N terms in the sum)
This is a geometric series with ratio 1/2, so the sum is equal to:
1/2
T(N) = N*[1 - (1/2)^(log_2 N)] / (1 - 1/2) = = [N - N/(2^log_2 N)] / 0.5 = 2^log_2 N = N = (N - 1) / 0.5
So T(N) is O(N).
T(N)
O(N)