Using the constraints to guess the intended complexity

Video lessonComing soon

1 The constraints are a hint

Every problem ends with a few lines like these:

1 ≤ n ≤ 10⁵
−10⁹ ≤ nums[i] ≤ 10⁹

Many students skip them. That wastes the most useful hint in the problem. The setter picked these limits so that the intended method passes and slower ones get Time Limit Exceeded (TLE).

The last two lessons went from code to its cost. This lesson goes the other way. We start from the limits, work out which costs fit, and only then look for a method.

2 From the size of n to a complexity

A computer does about 10⁸ simple steps a second, and time limits are usually one or two seconds. So a method has a budget of about 10⁸ steps. For each complexity, we ask how large n can get before it goes past that budget.

n up to aboutComplexity that fitsSteps at that nA typical method
10 or 11O(n!)10! ≈ 4·10⁶Trying every order
20O(2ⁿ)2²⁰ ≈ 10⁶Trying every group
500O(n³)500³ ≈ 10⁸Three nested loops
A few thousandO(n²)5000² ≈ 2.5·10⁷Two nested loops
10⁵ to 10⁶O(n log n) or O(n)10⁶ × 20 ≈ 2·10⁷Sorting, or one pass
10⁹ or moreO(log n) or O(1)about 30Binary search, or a formula

We do not need to learn the table by heart. Three facts rebuild it:

  • Powers of two. 2¹⁰ is about 10³, so 2²⁰ is about 10⁶.
  • Logs. log₂ of 10⁵ is about 17, and log₂ of 10⁹ is about 30.
  • Factorials. 10! is about 4·10⁶, and each step up multiplies by the next number.
n!, n = 103.6 × 10⁶2ⁿ, n = 2010⁶n³, n = 5001.3 × 10⁸n², n = 50002.5 × 10⁷n log n, n = 10⁶2 × 10⁷log n, n = 10⁹301 to 2 seconds
Each row's method at its largest n stays near the budget

With these, we check each row against 10⁸. For example, 10⁴ squared is exactly 10⁸, right at the budget, so n² is safe up to a few thousand.

3 One problem, three methods

A coach has n players, and heights[i] is the height of player i. She wants the largest team in which the tallest and the shortest player differ by at most d. We return the size of that team.

For heights = [8, 3, 10, 5, 4, 12] and d = 2, the answer is 3. The team is the players with heights 3, 4 and 5.

Three methods come to mind:

  • Try every group, 2ⁿ · n. We check every group of players and keep the largest one that works.
  • Each player as the shortest, n². For each player, we count the players whose heights lie from that height up to d more.
  • Sort first, n log n. Any player between a team's shortest and tallest can join it. So after sorting, we look for the longest run of neighbours whose ends differ by at most d.

4 Same problem, three sets of limits

Now we give the same problem three different limits:

Methodn ≤ 20n ≤ 5000n ≤ 10⁵
Every groupAcceptedTLETLE
Each playerAcceptedAcceptedTLE
Sort firstAcceptedAcceptedAccepted
  • n ≤ 20. Every method passes, so the simplest correct one is enough. A limit this small often means the setter expects us to try every group.
  • n ≤ 5000. Trying every group is out. n² is about 2.5·10⁷ steps and fits, so we do not need to sort.
  • n ≤ 10⁵. n² is around 10¹⁰ steps and gets TLE. The target is n log n, and that points to sorting.

The statement never changed. Only the limits did, and each set picked a different method.

5 What the value range tells us

The limits on the values matter too. First, they tell us whether a number can overflow.

A 32-bit int holds values up to about 2·10⁹. One height up to 10⁹ fits. The total of 10⁵ such heights is 10¹⁴, and that does not.

Most languages give no error when this happens. They quietly produce a wrong number, and the submission gets Wrong Answer. So we keep sums and products of large values in a 64-bit variable:

For the example it returns 42. Python and JavaScript need no change. The other five need a 64-bit total.

A line like "return the answer modulo 10⁹ + 7" is the same kind of hint. It says the true answer is too large for any integer type.

Second, the values tell us whether an array indexed by value fits. With values up to 10⁵, count[v] can hold how many times v appears. With values up to 10⁹, that array is far too big, and a hash map or a sort does the job instead.

6 The table is a guide, not a law

Far from its edges, the table is always right. 10¹⁰ steps fail in every language, and 10⁶ steps pass in every language. Near 10⁸, three more things decide the verdict:

  • The constant factor. Big-O drops the work inside each step, but the computer does not. A hash map lookup costs several array reads, so two O(n log n) methods can differ in speed by ten times.
  • The language. Python often runs plain loops ten times slower than C++. So n² at n = 10⁴ may pass in C++ and get TLE in Python.
  • Memory. A program has room for tens of millions of numbers, not billions. An n × n table fits at n = 1000 but not at n = 10⁵.

When our count lands near 10⁸, we treat it as a warning. We look for the next faster method, or keep the work inside each step light.

7 Using the constraints in an interview

In an interview, the constraints come before the method:

  1. Read the constraints right after the statement.
  2. Find the target from the largest n and the table.
  3. Say the brute force and its cost. If it fits, it may be the whole answer.
  4. Rule out every method slower than the target.
  5. Check the values for overflow, or for a range small enough to index an array.

For the team problem with n ≤ 10⁵, this takes twenty seconds out loud: "Trying every group is 2ⁿ, so that is out. Counting from each player is n², around 10¹⁰ steps, so that is out too. We need n log n, so we try sorting."

If the limits are missing, we ask: "How large can the input and the values get?"

The habit to keep: before looking for a method, write the target complexity next to the problem, and cross out every method that is slower.

Finished this lesson?

Mark it read and it is ticked on the roadmap and in the list.

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.