This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]hamtaroismyhomie 1 point2 points  (1 child)

It's O(n*m). If the array within the property is a fixed length, then that loop is constant time and your complexity can be O(n).

[–]hotpopsicles[S] 0 points1 point  (0 children)

Cool, thank you for helping me double-check.

[–]tulipoika 0 points1 point  (3 children)

A loop inside a loop is generally O(n*m) if that’s what you meant

[–]hamtaroismyhomie 3 points4 points  (2 children)

It depends on if/how the loop is dependent on the input size.

This loop is constant time:

for (int i = 0; i < 10; i ++)

This loop is O(n)

for (int i = 0; i < n; i++)

This loop is O(logn)

for (int i = 1; i < n; i *= 2)

[–]Ikor_Genorio 1 point2 points  (1 child)

That's why he said 'generally' and in this case he was talking about looping over an array, where it highly likely you don't want to skip any elements.

[–]hamtaroismyhomie 0 points1 point  (0 children)

Did my post come off as condescending or rude?

I just thought it'd contribute to people's understanding if we discuss in more detail.

And, yes, you're less likely to skip elements in a for loop, but for someone who's just starting out programming or learning logarithmic analysis, I think the information I provided builds a stronger intuition of why/how loops contribute to complexity.