Big-O and constraints
Big-O tells you how the work grows as the input grows. Read the input limits first, and they tell you which kind of solution can possibly be fast enough.
How to recognise it
- Every problem. Before designing anything, read the limits on n and on the values.
- The limits are large (n up to 10⁵ or more), which rules out trying every pair.
- Values can reach 10⁹ or more, which warns that sums and products may not fit in 32 bits.
- Your idea works on the example but you are not sure it will run in time on the biggest input.
The idea in one picture
Imagine two ways to find a name in a phone book of n pages. One reads every page from the start: double the book and
you double the work. The other opens the middle, decides which half the name is in, and repeats: double the book and you
add just one more step. Big-O is the language for that difference. It describes how the work grows as n grows,
and ignores small details like "3 steps per page" or "plus 5 at the start".
The common classes
| Big-O | Name | Example | n = 10⁵ means about |
|---|---|---|---|
| O(1) | constant | read nums[i] | 1 step |
| O(log n) | logarithmic | binary search | 17 steps |
| O(n) | linear | one pass over the array | 100,000 steps |
| O(n log n) | linearithmic | merge sort | 1.7 million steps |
| O(n²) | quadratic | every pair (i, j) | 10 billion steps |
| O(2ⁿ) | exponential | every subset | far too many |
Reading the limits
A judge typically runs around 10⁸ simple steps per second. So the limit on n tells you which row of the table you need:
| Largest n | Solutions that usually fit |
|---|---|
| up to about 20 | O(2ⁿ), trying every subset |
| up to about 500 | O(n³) |
| up to about 5,000 | O(n²) |
| up to about 10⁶ | O(n log n) or O(n) |
| up to 10⁹ or more | O(log n) or O(1), often maths |
Read the limits before designing. If n is 10⁵ and your plan checks every pair, you know it will be too slow
before you write a line.
How to count
for i from 0 to n - 1: # runs n times
for j from i + 1 to n - 1: # runs n - 1, n - 2, ... 1 times
compare nums[i] and nums[j]
# total about n²/2 comparisons, so O(n²)
Rules of thumb:
- Loops in sequence add: O(n) then O(n) is O(n).
- Loops nested inside each other multiply: O(n) inside O(n) is O(n²).
- A loop that halves (or doubles) its variable runs O(log n) times.
- Drop constants and smaller terms: 3n² + 5n + 7 is O(n²).
Space counts too
Extra memory is measured the same way. A few variables is O(1). A copy of the array is O(n). Recursion uses one stack
frame per open call, so a chain of n calls is O(n) space even with no arrays.
Values have limits too
A 32-bit int holds up to about 2.1 × 10⁹. If values can be 10⁹, adding two of them can overflow, and multiplying two
certainly does. Read the value limits and use 64-bit numbers where needed. Many problems in Maths basics turn on exactly
this.
Common mistakes
- Designing first and checking the limits last.
- Calling something O(n) when it hides a loop inside, such as
list.contains, joining strings with+in a loop, or slicing. - Forgetting the recursion stack when counting space.
- Ignoring value limits and overflowing a 32-bit integer.
Problems
No problems listed for this pattern yet.