In your first example, how many times will line 4 be executed? Write it as a function of n. Whatever your
formula is, that's the time complexity.
Assuming k starts at 1, the first time through k becomes 2. The second time through it becomes 4. The
third time through it becomes 8. It stops there. This is clearly logarithmic; line 4 will execute O( log2(n) )
times.
Now you do the second example. There is a subtlety there. First, figure out how many times the while()
loop will execute if n == 8. Then, figure out how many times the for() loop will execute.
Thanks... I figure out that while loop will execute 4 times, so after while loop exited the n will be less than zero. So finally for loop will execute only once...