Big O Notation for two non-nested loops

What would the Big O designation mean for two loops that are not nested?

Example:

for(int i=0; i<n; i++){ System.out.println(i); } for(int j=0; j<n; j++){ System.out.println(j); } 
+5
source share
4 answers

Linear

 O(n) + O(n) = 2*O(n) = O(n) 

It does not matter how many nested loops you have (if this number is a constant and does not depend on n ), the complexity will be linear and will be equal to the maximum number of iterations in the loop.

+14
source

Technically, this algorithm still works in O (n) time.

While the number of iterations increases by 2 for each increase in n , the time, which is still increasing, increases with the speed linear , therefore, in O (n) time.

+5
source

It will be O(n) + O(n) ==> Effectively O(n) , since we do not store constant values.

+3
source

This will be O (2n) because you are doing n + n = 2n iterations.

O (2n) is essentially equivalent to O (n) since 2 is a constant.

+1
source

Source: https://habr.com/ru/post/1239008/


All Articles