Worst-case time complexity of il-else block that iterates two disjoint sets

23 Views Asked by At

I am estimating the worst-case time complexity of this piece of code:

if (condition) {
  // loop over A
} else {
  // loop over B
}

such that A and B are disjoint sets and |A| + |B| = C. In my opinion, the worst-case time complexity is O(max(|A|, |B|)) = O(C). Could you please tell me if it is correct?

Also a side question, if I have a complexity like O(N(1 + M)), do I have the right to drop 1 and keep only O(NM) as the final complexity?

I apologize if my question is too basic. I haven't touched complexity analysis for a long time.

Thank you!

1

There are 1 best solutions below

1
On BEST ANSWER

I am guessing that it may depend on the condition, and some probabilities of A and B hitting the condition. For example, if the condition is A.length > B.length then it would be O(max(|A|, |B|).

Also for your side question, you definitely can drop the N in the O(N(1+M)) to have the final be O(NM).