I'm getting started with complexity and I wanted to know why the example given below is O(n^2) and not O(n), would you guys help me? I need it fast for an exam.
l1 = []
for e in range(0, n):
if e in range(n, 2n):
l1.append(e**3)
I'm getting started with complexity and I wanted to know why the example given below is O(n^2) and not O(n), would you guys help me? I need it fast for an exam.
l1 = []
for e in range(0, n):
if e in range(n, 2n):
l1.append(e**3)
I think the
if e in range(n, 2n):
is supposed to be
for e in range(n, 2n):
or even
for j in range(n, 2n):
If this is the case, then the O(n^2) complexity would make sense since are n iterations happening n times. Meaning the complexity is n * n = n^2.
Otherwise, the loop simply runs n times (range of 0,n) and does not execute
if e in range(n, 2n)
since e would not fall within this range.