JavaScript
x
8
1
result = []
2
for i in n:
3
for jj in range(len(m)):
4
if jj < 3:
5
result.append((n,m))
6
else:
7
jj = len(m)
8
- m, n are two python array
- inner loop is maximum 3
Thinking
- O(n)? because inner looping is in a fixed amount
- O(n*m)?
- O(n*3)? this is not the correct way :(
What is the correct O time complexity for this?
Advertisement
Answer
Time complexity of the statement inside the inner loop is in O(1)
. Because, it is just only one comparison and one variable assignment, and computing the len(m)
is done in O(1)
. The remaining is straightforward: two nested loop with n
and m
iterations. Therefore, the time complexity is O(m * n)
.