Skip to content

Arranging Coins

EasyBinary Search
Decode it first. Clues, constraints, pattern and plan before any code.

You have n coins and build a staircase: row 1 has 1 coin, row 2 has 2 coins, row k has k coins. The last row may be left incomplete. Return the number of complete rows you can build.

n = 5   →  2   (rows of 1 and 2 use 3 coins; 2 left over is not enough for row 3)
n = 8   →  3   (1 + 2 + 3 = 6; 2 left over)
n = 1   →  1

The first k rows need k * (k + 1) / 2 coins in total.

Constraints

  • 1 <= n <= 2^31 - 1