Time ans space complexity Problem 1

The answer of this should be log N instead of n

Hey @Alfred
Please share the question

nd 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++; O(log N) O(N) O(N*log N) O(N^2)

This question I am confused little bit

Hey @Alfred
in iteration 1 inner loop will run -> N time
in iteration 2 inner loop will run -> N/2 time
in iteration 3 inner loop will run -> N/2^2 time
in iteration 4 inner loop will run -> N/2^3 time

if add them u will get
N+ N/2+N/2^2 +N/2^3…+ N/2^K …+ 1

N( 1+1/2+1/4+1/8+1/16…) ==N(1/(1-1/2) ) [infinite gp sum] == 2N ==O(N)

Okk so it’s approximately equal to n therefore bigOn

Yeah …

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.