0

i wanted to know what is the complexity of doing two loops when they both do the same amount of loops, like this:

for(i=0;i<n;i++)
{

}

for(i=0;i<n;i++)
{

}

is it O(n+n) = O(n)?

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
noor napso
  • 113
  • 5
  • 2
    would be O(2n) but it's actually O(n) since in time complexity you ignore the constants. – lag Feb 07 '20 at 15:56
  • Yes, $O(n) + O(n) = O(n)$. Check out e.g. Hildebrand's "Short course on asymptotics" (available on his webpage) for the gory details (more geared to analysis, but very relevant). – vonbrand Feb 07 '20 at 15:58
  • Does this answer your question? [Which algorithm is faster O(N) or O(2N)?](https://stackoverflow.com/questions/25777714/which-algorithm-is-faster-on-or-o2n) – Bee Feb 07 '20 at 16:01

0 Answers0