Why option D i s incorrect


#1

It will run infinite times since condition i is always greater than -1. Please correct me if I am wrong.


#2

you are right, it has infinite loop.


#3

Its like the concept of half-life! It may reach integer zero but never negative.


#4

I almost didn’t notice the Infinite Loop


#5

Divide by two will never get you to negative value, that’s why solution D will run infinitely.


#6

Let n=8;

  1. i=8 8 >- 1 true
  2. i=8/2=4 4 >- 1 true
  3. i=4/2=2 2 > -1 true
  4. i=2/2=0 0 > -1 true
  5. i=0/2=0 0 > -1 true
  6. i=0/2=0 0 > -1 true…
    This loop continues and won’t terminate…

#7

here :—
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…
:blush::blush::blush::blush::blush:


#8

You are right as in the worst case i becomes 0 but never negative,


#9

The loop will run infinite no of times. As " i " will never become negative in this case.


#10

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.


#11

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 //


#12

bcz it will make the itetaration in infinite loop.