It will run infinite times since condition i is always greater than -1. Please correct me if I am wrong.
you are right, it has infinite loop.
Its like the concept of half-life! It may reach integer zero but never negative.
I almost didn’t notice the Infinite Loop
Divide by two will never get you to negative value, that’s why solution D will run infinitely.
- i=8 8 >- 1 true
- i=8/2=4 4 >- 1 true
- i=4/2=2 2 > -1 true
- i=2/2=0 0 > -1 true
- i=0/2=0 0 > -1 true
- i=0/2=0 0 > -1 true…
This loop continues and won’t terminate…
4. i=2/2 is 1 which is > -1 (true)
now 5. i=1/2 is 0 0>-1 true…
and then loop continues and won’t terminate…
You are right as in the worst case i becomes 0 but never negative,
The loop will run infinite no of times. As " i " will never become negative in this case.
let take a value for n suppose n=7,now then update operator(i/=2) occur each time its divide n by 2 so it becomes n=7(initially) ->3 ->1 ->0 it will never go below 0 and as we know 0>-1 so the condition satisfy always and it becomes an infinite loop.
Listen do a test for sample case where n=16;
if first for loop i=0 to i<16 // This loop will execute for 16 times // T.C O(N).
if second for loop i // This loop will execute for 8times // T.C O(N/2).
if third for loop // This loop will execute for 5 times // T.C O(logN).
if fourth for loop // This loop will execute for infinite times //
bcz it will make the itetaration in infinite loop.