Reading the Constraints

Assessment problems are set by people who choose nn deliberately. The bound is not flavour text, it is the author telling you which complexity class they intend, and once you can read it the space of plausible approaches collapses from everything you know to two or three things.

The ladder

These are the bounds problem authors set, and they are calibrated for a compiled language at a few seconds:

Bound on nn Target Usually means
n≤12n \le 12 O(n!)O(n!) Permutations, brute force over orderings
n≤25n \le 25 O(2n)O(2^n) Subsets, meet in the middle
n≤500n \le 500 O(n3)O(n^3) Floyd-Warshall, interval dynamic programming
n≤5000n \le 5000 O(n2)O(n^2) Pairwise dynamic programming, nested scan
n≤105n \le 10^5 O(nlog⁡n)O(n \log n) Sorting, heap, binary search, balanced tree
n≤106n \le 10^6 O(n)O(n) One pass, hash map, two pointers, prefix sums
n≤1018n \le 10^{18} O(log⁡n)O(\log n) Binary search, arithmetic, fast exponentiation

The rest of this lesson is for subscribers

Unlock every lesson in Programming for Quantitative Developers, and every other premium course.

Subscribe to continue

Test your knowledge

Questions are only available to subscribers.

Keep reading Programming for Quantitative Developers

33 lessons in this course, and every other premium course, on one subscription.

  • Every lesson in every course, with the worked examples and interactive simulators
  • Graded questions on every lesson, with explanations for the wrong answers as well as the right one
  • The trainers, timed assessments and brainteaser library that go with them