Here I come again with basic questions 🙁
If I have the next pseudo-code:
iterate over set (A)
//some *O(1)* operations
iterate over set (B)
//another *O(1)* operations
From what I have learned, the time would be O(numberOfElementsInA + numberOfElementsInB)
However, If I know that B is subset of A and numberOfElementsInA is always greater or equal than
numberOfElementsInB, can I simplify the time by writing just O(numberOfElementsInA) ?
Yes, you are correct.
This is because
numberOfElementsInA + numberOfElementsInB <= 2 * numberOfElementsInA, and from definition of big O notation it makes itO(numberOfElementsInA)(withc=2, and for everyN)EDIT: To be exact, each loop is
O(numberOfElementsInSet_i)– thus there are constantsc_i, N_ifor each loop such thatT(loop_i) <= numberOfElementsInSet_i * c_ifor eachnumberOfElementsInSet_i > N_i.Thus:
And now we have a formal proof that the loops together are also
O(numberOfElementsInSet_1)withN = max{N1,N2}andc = max{c_1,c_2} * 2