ADS · Complexity & Big-O
Given the following function, determine its time complexity and explain your reasoning.
def mystery(n):
total = 0
for i in range(n): # loop A
for j in range(n): # loop B
total += 1
for k in range(n): # loop C
total += k
return total
What is the Big-O complexity? What would change if loop B ran range(i) instead of range(n)?
Break it down loop by loop and then combine.
n times → O(n²)n times → O(n)For the variant where loop B runs range(i):
Try solving it first. The solution will wait.