'Time Complexity of Separate Equal Length Arrays
Just wondering what the time complexity of the below algorithm is.
Y = [3, 2, 1]
F = [4, 5, 6]
V = [50, 8, 1]
def algo(Y, F, V):
sums = set({})
for i in Y:
for j in F:
sums.add(i + j)
for i in V:
for j in sums:
if i == j:
return True
return False
I assume its n^2, but just want to be sure... and know why it isn't if it isn't. Thanks!
Solution 1:[1]
sums is at most size n^2, so the below nested loops is actually worst case looping through n^3 elements. Therefore algo actually runs in O(n^3).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Harry Duffy |
