Arranging Coins
Easy
Topics
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
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.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
- Binary search
kover[0, n]. - For each
mid, comparemid*(mid+1)/2againstn; if it fits, search higher, else lower. - 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)/2withnup 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. Withnat its maximum the product far exceeds a 32-bit int; uselong. - Returning
loinstead ofhi. With this loop shape the pointers cross, andhiholds the last value that fit. - Trusting the
sqrtclosed 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 hiimport java.util.*;
class Solution {
public int arrangeCoins(int n) {
long lo = 0, hi = n;
while (lo <= hi) {
long mid = (lo + hi) / 2;
long need = mid * (mid + 1) / 2;
if (need <= n) lo = mid + 1;
else hi = mid - 1;
}
return (int) hi;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int arrangeCoins(int n) {
long long lo = 0, hi = n;
while (lo <= hi) {
long long mid = (lo + hi) / 2;
long long need = mid * (mid + 1) / 2;
if (need <= n) lo = mid + 1;
else hi = mid - 1;
}
return (int) hi;
}
};/**
* @param {number} n
* @return {number}
*/ var arrangeCoins = function(n) {
let lo = 0, hi = n;
while (lo <= hi) {
const mid = Math.floor((lo + hi) / 2);
const need = mid * (mid + 1) / 2;
if (need <= n) lo = mid + 1;
else hi = mid - 1;
}
return hi;
};