Every good problem statement ends with a constraints section, and most beginners skip it. That's a mistake: the constraints quietly tell you how fast your solution needs to be — and therefore which approaches are even worth trying.
A typical judge (and a typical interviewer's expectation) allows roughly one second, and a computer does somewhere around 100 million simple operations in that time. So plug the largest n into your algorithm's complexity and check whether the result stays under about 10⁸.
n = 100,000 (10⁵)
O(n²) → 10¹⁰ steps ✗ about 100 seconds: too slow
O(n log n) → 1.7 × 10⁶ ✓
O(n) → 10⁵ ✓Free account
Sign up to read the rest of this lesson: 4 more sections and 1 drawing.
Still to come