0

I know that complexity of two serial loops with the same number of iterations is O(n), as stated here, but what if the loops are based on different inputs? for example:

for(i;i<m;i++){
   //code
}
for(y;y<n;y++){
   //code
}

It would be O(m+n)?

Community
  • 1
  • 1
Rafael Angarita
  • 777
  • 10
  • 27

1 Answers1

4

Yes, absolutely :)

The first loop, if it's not empty, has a multiple of m operations.

The second loop has a multiple of n operations.

Using both one after the other gives you O(m+n).

alestanis
  • 21,519
  • 4
  • 48
  • 67