Estimating Time Limits

Will brute force be fast enough?

Modern computers do about 10810^8 simple operations per second.

Rule of thumb for 1-second limit:

  • O(n)O(n): n≤108n \leq 10^8
  • O(nlog⁡n)O(n \log n): n≤107n \leq 10^7
  • O(n2)O(n^2): n≤104n \leq 10^4
  • O(n3)O(n^3): n≤500n \leq 500
  • O(2n)O(2^n): n≤25n \leq 25
  • O(n!)O(n!): n≤10n \leq 10

Example: If n=20n = 20 and you need O(2n)O(2^n), that's ≈106\approx 10^6 operations. Should pass in well under a second.

Always check constraints first. If brute force fits, use it. Simple code has fewer bugs.