What is the time complexity of the following loop?
for(int i=1; i<=n; i=i*2){
    System.out.println("*");
}
is it logarithmic?
The algorithm as shown would be O(log n)
Since the number of iterations would be log2(n), or log10(n)/log10(2)
It's O(log n) because you are doubling the value of i on each iteration: 1, 2, 4, 8, ...
If n is 2x the loop will terminate after log2(2x) + 1 = x + 1 iterations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With