0
What is the time complexity of the following code:
int a=0, i=N; While (i>0){ a+=i; i/=2; } Options: 1. 0(N) 2.0(sqrt(N)) 3.0(N/2) 4.0(logN) Answer: Option: Explanation:
5 Respostas
+ 2
O(logN) probably since everytime while loop runs, number gets divided into half so for a number like 32 it runs 5 times which is:
2 to the power of 5=32
+ 1
division by 2 or greater ends up with logN
To know more , you might see this
https://code.sololearn.com/cAAmIcha1GsN/?ref=app
0
@Abhay thanks