Space time complexity Quiz Q1

Quiz question 1:

Find the time complexity of the given code.

int count = 0;
for (int i = N; i > 0; i /= 2)
for (int j = 0; j < i; j++)
count++;

The soln itself says that the innermost loop will take n + n/2 + n/4 +…1

So how is the ans O(N) ?

 n + n/2 + n/4 + n/8 + ... + 1  
= n * (1 + 1/2 + 1/4 + 1/8 + ...) 
 = 2n

n + n/2 + n/4 + … 1

So time complexity T(n) can be written as

T(n) = O(n + n/2 + n/4 + … 1) = O(n)

i hope this helps
if yes hit a like and don’t forgot to mark doubt as resolved :grinning:
if you have more doubts regarding this feel free to ask

please mark doubt as resolved

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.