1

Can someone explain how I wouldfigure out the big $O$ notation for this expression?

$$n+(n-1)+(n-2)...+(n-(n-1))$$

My thoughts is that it's $\mathcal{O}(n)$ because I think of all the other terms as smaller constants but I am not sure.

What if the operation had to be done twice? That is,

$$[n+(n-1)+(n-2)...(n-(n-1))] \times 2$$

Thanks

Calvin Khor
  • 34,903

1 Answers1

1

You are asking about the asymptotic behaviour of $$ \sum_{k=1}^n k $$ as $nā†’āˆž$. We can write this as

$$ \sum_{k=1}^n k = \frac{n(n+1) }{2} = \frac{n^2}{2} +\mathcal{O}(n) = \mathcal{O}(n^2)$$

Doing it twice doubles the 'time taken'; this amounts to multiplying the $\mathcal{O}$ constant by 2, which is ok. That is, it is still $\mathcal{O}(n^2)$.

Calvin Khor
  • 34,903