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
n12n \le 12 O(n!)O(n!) Permutations, brute force over orderings
n25n \le 25 O(2n)O(2^n) Subsets, meet in the middle
n500n \le 500 O(n3)O(n^3) Floyd-Warshall, interval dynamic programming
n5000n \le 5000 O(n2)O(n^2) Pairwise dynamic programming, nested scan
n105n \le 10^5 O(nlogn)O(n \log n) Sorting, heap, binary search, balanced tree
n106n \le 10^6 O(n)O(n) One pass, hash map, two pointers, prefix sums
n1018n \le 10^{18} O(logn)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

25 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