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): n108n \leq 10^8
  • O(nlogn)O(n \log n): n107n \leq 10^7
  • O(n2)O(n^2): n104n \leq 10^4
  • O(n3)O(n^3): n500n \leq 500
  • O(2n)O(2^n): n25n \leq 25
  • O(n!)O(n!): n10n \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.