What is the time complexity of the following nested for
loop please?
Edit. I think the answer to this question hinges on another question, to which I don't know if there is a "canonical" answer.
That question is whether the n
in big-O expressions such as O(n)
, O(n^2)
refers explicitly to an input parameter called n
, or to a general value representing the size of the input.
Some of the answers given so far seem to contradict the answer given here: https://stackoverflow.com/a/23361893/3042018 I would appreciate some more clarity if possible.
for i in range(n):
for j in range(m):
print(i, j) # Output statement occurs n * m times.
I'm thinking O(n^2) as each loop is O(n), but I'm wondering if it might be O(nm), and if/whether these are in fact the same thing.