Arranging Coins (LeetCode 441): Solution and Explanation

Easy
Math
Binary search

Build a staircase where row i holds i coins. Given n coins, return how many complete rows you can build.

Intuition

A staircase with k full rows holds 1 + 2 + ... + k = k(k+1)/2 coins. That number only grows as k grows, so the rows that fit form a prefix: every k up to some limit fits, and every k beyond it does not.

Whenever a yes/no question flips exactly once along a sorted range, binary search finds the flip point. Here the question is "does k(k+1)/2 <= n?", and the answer is the last k where it is still yes.

Searching k in [0, n] takes about 31 steps even for n = 2^31 - 1. The only trap is overflow: k(k+1) for k near 2^31 does not fit in a 32-bit integer, so the multiplication must be done in 64 bits.

Approach: Binary search on k

  1. Set lo = 0 and hi = n.
  2. While lo <= hi, take k = (lo + hi) // 2 and compute need = k * (k + 1) / 2 in 64-bit arithmetic.
  3. If need == n, k rows use every coin exactly: return k.
  4. If need < n, k rows fit, so try more: lo = k + 1.
  5. Otherwise there are not enough coins: hi = k - 1.
  6. When the loop ends, hi is the largest k that fits. Return it.

Step-by-step example

n = 8
  1. lo = 0, hi = 8. k = 4 needs 10 coins, more than 8, so hi = 3.
  2. k = 1 needs 1 coin, which fits, so lo = 2.
  3. k = 2 needs 3 coins, which fits, so lo = 3.
  4. k = 3 needs 6 coins, which fits, so lo = 4.
  5. Now lo = 4 > hi = 3, so the loop stops and the answer is hi = 3.

Code: Python, Java, C++

1class Solution:
2    def arrangeCoins(self, n: int) -> int:
3        lo, hi = 0, n
4        while lo <= hi:
5            k = (lo + hi) // 2
6            need = k * (k + 1) // 2
7            if need == n:
8                return k
9            if need < n:
10                lo = k + 1
11            else:
12                hi = k - 1
13        return hi
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(log n)

The search range [0, n] halves every iteration, so at most about 32 iterations run for 32-bit n.

Space

O(1)

Only lo, hi, k and need are stored.

Other approaches

Closed form with the quadratic formula (time O(1), space O(1))

Solve k(k+1)/2 <= n for k: k = floor((sqrt(8n + 1) - 1) / 2). Use an integer square root (math.isqrt in Python) or check the result with one adjustment step, since floating-point sqrt can be off by one for large n.

Subtract row by row (time O(sqrt n), space O(1))

Remove 1, then 2, then 3 coins, counting rows until the next row cannot be paid for. It is simple but takes about sqrt(2n) steps, around 65,000 for the largest input.

Common mistakes

  • Computing k * (k + 1) in 32-bit integers. For k around 2^31 it overflows and the comparison becomes wrong.
  • Returning lo instead of hi after the loop. When the loop ends, lo is one past the answer.
  • Using floating-point sqrt without a correction step. For n near 2^31, rounding can give an answer one too large or too small.

Frequently asked questions

Why does the search start with hi = n instead of something smaller?

n is a safe upper bound since k rows need at least k coins. A tighter bound such as sqrt(2n) + 1 works too and saves a few iterations, but the gain is tiny.

Is the closed form better in an interview?

It is O(1) and short, but you need to explain the floating-point risk. Many interviewers prefer the binary search because it shows the monotonic-predicate pattern and handles overflow explicitly.

What if n is exactly a triangular number?

Then some k has k(k+1)/2 == n and every coin is used. The early return handles it, and without it the loop would still end with hi = k.