July 2022
Intermediate to advanced
496 pages
11h 18m
English
Find the time complexity of the following Python snippets:
i=1
while(i<n):
i*=2
print("data")
i =n
while(i>0):
print("complexity")
i/ = 2
for i in range(1,n):
j = i
while(j<n):
j*=2
i=1
while(i<n):
print("python")
i = i**2
O(log(n)). As we are multiplying the integer i by 2 in each step there will be exactly log(n) steps. (1, 2, 4, …… till n).
O(log(n)). As we are dividing the integer i by 2 in each step there will be exactly log(n) steps. (n, n/2, n/4, …… till 1).
n times for each i in the outer loop, while the inner while loop will run log(i) times because ...