# How come O(n) + O(logn) = O(logn)

How come O(n) + O(logn) = O(logn)?

When talking for example about an algorithm that has two operations. One of them takes O(n) and the other O(logn) and in the end we say that the total complexity is O(logn). It doesn't make sense to me since n is bigger than logn.

• Where did you come across this statement? – Juho Dec 2 '18 at 10:30
• $O(n)+O(\log n)=O(n)$, not $O(\log n)$, as your intuition tells you. If you read $O(\log n)$ somewhere, that was a mistake. (Assuming you did not misread something, of course) – chi Dec 2 '18 at 11:39
• Use the definitions. – Raphael Dec 2 '18 at 15:02
• It doesn't make sense to you because it is wrong. – gnasher729 Dec 2 '18 at 23:17

As Chi say to you, if you have in a program two operations, one $$\mathcal{O}(log{}n)$$ and another $$\mathcal{O}(n)$$ you must take a superior bound, and your result will be $$\mathcal{O}(n)$$ in this case. you can see here the complexity graph. If you say, that one operation take time $$\mathcal{O}(n)$$ , and nobody other operations take more of this, you can be at least $$\mathcal{O}(n)$$ time complexity. If just one operation are over $$\mathcal{O}(n)$$, the complexity will be change in new bound (and so on).

P.s Sum two bound, will maintain the most higher, multiply increse time complexity, like two neasted loop of $$\mathcal{O}(n)$$ will give you $$\mathcal{O}(n^2)$$ complexity.

I hope that this will help you.

• Using plots to explain asymptotics is a bad idea. – Raphael Dec 2 '18 at 15:03
• Yes i got the point @Raphael but this example are very basic. This fuction all already know, and i hope the user know that O(n) is < than O(n^2). But for other example, the graph are very bad way to understand trend of the functions. Thanks for this suggestion. – theantomc Dec 2 '18 at 15:40
• Basic examples/exercises are designed to teach the right tools in a circumstance the learner can handle. It doesn't make much sense to use different tools that will fail in any non-trivial circumstance. – Raphael Dec 2 '18 at 19:39