Arranging Coins

Easy

Topics
MathBinary Search

You have n coins and want to build a staircase where row i holds exactly i coins. The last row may be incomplete. Return the number of complete rows you can build.

Example 1

●··●●·●●·
Row k needs k coins, so k complete rows need k(k+1)/2. The answer is the largest k with that total ≤ 5, which is 2 — shaded above.
A staircase where row k holds k coins; shaded rows are the complete ones.
Input:  n = 5
Output: 2

Example 2

●···●●··●●●·●●··
Input:  n = 8
Output: 3

Example 3

●···
Input:  n = 1
Output: 1

Constraints

  • 1 <= n <= 2^31 - 1
Editorial — intuition, approach, complexity and pitfalls

Intuition

Row k needs k coins, so k complete rows need 1+2+...+k = k(k+1)/2. We want the largest k whose triangular number fits in n.

Counting rows one at a time by subtracting is O(sqrt n) and perfectly fine. But the predicate "does k rows fit" is monotonic — if k fits then every smaller count fits, and once it stops fitting it never fits again. Monotonic predicates are what binary search is for, so search the row count directly.

There is also a closed form: solving k(k+1)/2 <= n gives k = floor((sqrt(8n+1) - 1) / 2). It is O(1) and worth knowing, with the caveat that floating-point sqrt on inputs near 2^31 can land one off and needs a correction step — which is why binary search is the safer answer in an interview.

Approach

  1. Binary search k over [0, n].
  2. For each mid, compare mid*(mid+1)/2 against n; if it fits, search higher, else lower.
  3. Return hi, which ends on the last value that fit.

Dry Run

n = 8
lo=0 hi=8   mid=4  need=10 > 8   -> hi=3
lo=0 hi=3   mid=1  need=1  <= 8  -> lo=2
lo=2 hi=3   mid=2  need=3  <= 8  -> lo=3
lo=3 hi=3   mid=3  need=6  <= 8  -> lo=4
lo > hi, return hi = 3

check: 3 full rows cost 6, a 4th would need 4 more and only 2 remain  ✓

Complexity

  • Time — O(log n): the search space is [0, n] and each probe is constant work. Against O(sqrt n) for subtracting row by row.
  • Space — O(1): three integers. Note mid*(mid+1)/2 with n up to 2^31-1 gives a product near 2^62, so this must be computed in 64-bit in Java and C++ — Python widens automatically, which hides the problem until you port it.

Common Pitfalls

  • Overflow in mid*(mid+1)/2. With n at its maximum the product far exceeds a 32-bit int; use long.
  • Returning lo instead of hi. With this loop shape the pointers cross, and hi holds the last value that fit.
  • Trusting the sqrt closed form without a correction. Double precision runs out before 2^31, so verify the candidate and step down once if it overshoots.
  • Searching the coin count rather than the row count. The answer is a number of rows; the monotonic predicate is over rows.

Full Solution — all languages

class Solution:
    def arrangeCoins(self, n):
        lo, hi = 0, n
        while lo <= hi:
            mid = (lo + hi) // 2
            need = mid * (mid + 1) // 2
            if need <= n:
                lo = mid + 1
            else:
                hi = mid - 1
        return hi
SensAI
Beta
Tap the mic to stop voice mode