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:

7th Jul 2020, 1:06 PM
Precious Chisom Obisike
Precious Chisom Obisike - avatar
3 Answers
+ 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
7th Jul 2020, 1:21 PM
Abhay
Abhay - avatar
+ 1
division by 2 or greater ends up with logN To know more , you might see this https://code.sololearn.com/cAAmIcha1GsN/?ref=app
7th Jul 2020, 5:32 PM
Hima
Hima - avatar
0
@Abhay thanks
7th Jul 2020, 1:26 PM
Precious Chisom Obisike
Precious Chisom Obisike - avatar