Let m
be the length of input
and n
be the length of input2
. The complexity of the first loop is O(m)
and the complexity of the second one is O(n)
. Whenever you are facing sequential code, complexities are summed up (they would've been multiplied if the loops were nested instead of sequential), giving us a total O(m + n)
and that is the exact upper bound and is obviously linear in the size of the arguments.
Now, m + n <= 2 * max(m, n)
so you may say that the function is also O(2 * max(m, n))
but we can of course drop the constant so that gives us O(max(m, n))
. Let us assume that n
is always greater than m
(it's not a big deal as it doesn't change much). That gives us O(n)
, so indeed you can say that the algorithm is O(n)
but a more precise notation would be O(m + n)
.