Big O Notation for two non-nested loops
Technically this algorithm still operates in O(n) time.
While the number of iterations increases by 2 for each increase in n
, the time taken still increases at a linear rate, thus, in O(n) time.
Linear
O(n) + O(n) = 2*O(n) = O(n)
It does not matter how many non nested loops do you have (if this number is a constant and does not depends on n
) the complexity would be linear and would equal to the maximum number of iterations in the loop.
It would be O(2n) because you run n+n = 2n iterations.
O(2n) is essentially equivalent to O(n) as 2 is a constant.