Arranging Coins
Find how many full rows a staircase of n coins can have, when row i holds i coins.
Given nii
Example 1
Output: 2
Explanation: rows 1 and 2 use 3 coins, and the 2 coins left cannot fill row 3.
Example 2
Output: 3
Explanation: rows 1, 2 and 3 use 6 coins, and the 2 coins left cannot fill row 4.
The Idea
Picture laying coins on a table, row under row, each row one coin longer than the row above. Take the 8 coins of Example 2. Row 1 takes 1 coin, row 2 takes 2 and row 3 takes 3, which uses 6 coins. The 2 coins left go into row 4, but row 4 needs 4, so it stays unfinished. The answer is 3.
The obvious way to solve it is to do just that: lay the rows one by one and subtract their coins until the next row no longer fits. For 8 coins that is quick. For 2,147,483,647 coins, the largest Java int
Turn the question around. Instead of laying rows, pick any number of rows, call it kk
On the left are the 8 coins, with two empty spots in row 4. On the right, the table lists the coins needed for k
That pattern holds for any number of coins, because every extra row needs more coins than the rows before it. Once some kkk
Finding the point where a yes-or-no answer flips is what binary search is good at. It checks the middle of a range of candidates and throws away the half that cannot hold the answer, so every check halves the range. Here the candidates are not the items of an array but the possible numbers of rows.
Solution Approach
Turning that idea into code means settling four things: how to count the coins that k
Coins needed for k rows
How many coins do kk
Each column, such as 1 + 4 or 2 + 3, adds up to 5, which is k + 1kk(k + 1)k(k + 1) / 2
The range of row counts
Where can the answer be? At least 0 rows, which is what 0 coins give. At most nlo = 0hi = n
The search runs as a loop, and each pass of the loop takes a row count midmidmidlo = midmidmidhi = mid - 1
Rounding the middle up
Which middle do we take when the range has two values, say lo = 2hi = 3(lo + hi) / 2lo = midlo
Rounding up, with mid = lo + (hi - lo + 1) / 2lohilo = 0hi = 8mid = 4hi = 3mid = 2lo = 2mid = 3lo = 3hi
Counting in long
Can the coin count itself go wrong? The product mid * (mid + 1)intnint
For the largest nmidmid * (mid + 1)intlonglohimidlong
Optional: a formula instead of a search
Is a search needed at all? Solving k(k + 1) / 2 = nk(int) ((Math.sqrt(8.0 * n + 1) - 1) / 2)intn
Java Solution
The method arrangeCoins
- First, set
andlo = 0 , bothhi = n : the smallest and largest possible numbers of rows.long - Loop while
is less thanlo . In each pass, computehi , the middle of the range rounded up.mid = lo + (hi - lo + 1) / 2 - Check whether
rows fit, withmid . If they fit, setmid * (mid + 1) / 2 <= n ; if not, setlo = mid .hi = mid - 1 - When the loop ends,
equalslo , and that value is the answer. Return it as anhi .int
The mainint
public class ArrangingCoins {
// Largest k with 1 + 2 + ... + k = k(k + 1) / 2 <= n
public static int arrangeCoins(int n) {
long lo = 0;
long hi = n;
while (lo < hi) {
long mid = lo + (hi - lo + 1) / 2; // round up, so lo = mid always makes progress
if (mid * (mid + 1) / 2 <= n) {
lo = mid; // mid full rows fit
} else {
hi = mid - 1; // mid rows need too many coins
}
}
return (int) lo;
}
public static void main(String[] args) {
int[] tests = {5, 8, 6, 1, 0, 2147483647};
for (int n : tests) {
System.out.println("n = " + n + " -> " + arrangeCoins(n));
}
}
}
Running it prints:
n = 5 -> 2
n = 8 -> 3
n = 6 -> 3
n = 1 -> 1
n = 0 -> 0
n = 2147483647 -> 65535
Look at n = 6 -> 3<=n = 0lohi
The last line says 2,147,483,647 coins make 65,535 full rows. Those rows use 2,147,450,880 coins, and row 65,536 would need 65,536 more, which is more than the 32,767 coins left. Getting there took 31 passes of the loop, against 65,535 steps for laying the rows one by one.
To see the rounding problem for real, change the midlo + (hi - lo) / 2n = 5n = 8n = 3lo = 2hi = 3
The original implementation,
Complexity
The work is one binary search over the row counts 0 to n
- Time: O(log n), because each pass halves the range of
possible row counts. For the largestn + 1 that is 31 passes.int - Space: O(1), because the method keeps only
,lo andhi , however many coins there are.mid