Can u explain me time complexity of this q

Hey @anantgovil123

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

So we have to find time complexity of this so lets tart with outer loop
for 1st iteration of outer loop i=n ,inner loop run n times
then for next iteration i=n/2 and inner loop run n/2 times
for next iteration i=n/4 and inner loop run n/4 times and so on
So total operations= n+n/2+n/4+n/8+ … 1 == n*(1+1/2+1/4+1/8+1/16+…+) <= n*(gp of 1/2 infinite temrs)) <=n* (1/(1-1/2)) [sum of infinite gp a/(1-r)] <=2*n

Hence its O(n)
If this resolves your query then please mark it as resolved :slight_smile:

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.