BeginnerHabits & complexity
Read the constraints before you code
The input size tells you how fast your solution must be. n ≤ 10⁵ means an O(n²) double loop will time out.
The idea
Online judges run roughly 10⁸ simple steps per second. Multiply out your idea's Big O with the largest n and you know whether it can pass before writing a line.
Read it backwards too: the constraint hints at the intended solution. Tiny n invites brute force; huge n demands O(n) or O(n log n).
Worked example
Picking an approach from n
- n ≤ 20 → 2²⁰ ≈ 1 million: trying every subset is fine.
- n ≤ 2,000 → n² = 4 million: a double loop is fine.
- n ≤ 100,000 → n² = 10 billion: too slow. Aim for O(n log n) (sort, heap, binary search) or O(n) (hash map, two pointers).
How to spot it
- Every problem has constraints at the bottom. Read them first.
Common mistakes
- Coding the brute force for n = 10⁵ and only learning it's too slow from the judge.
- Ignoring value ranges, which decide whether a counting array or overflow matter.
Practise it
Go further
Up next for you · Beginner
“Have I seen this?” is a set →