0

Say I solve a problem that has a time complexity linear to the problem set, but on top of that the program used a few 0(1)s to do some function. Would I then have to add all the O(1)s to the O(n) to get the actual Big-Oh?

John
  • 25
  • 6

2 Answers2

1

No, the O(1) are not added together as they consume constant space/time irrespective of the inputs to the algorithm or program.

See How to find time complexity of an algorithm for details.

Community
  • 1
  • 1
HappyTown
  • 5,464
  • 7
  • 34
  • 48
0

You may add the constant terms if you like, but this is of no use. Indeed, O(n) and O(n+27) are equivalent (as well as O(43n-52), O(n/9+1023√n), O(n+log³n), O(√(n²+1))...).

Yves Daoust
  • 53,540
  • 8
  • 41
  • 94