Skip to content
Decoder

Arranging Coins

Easy

Problem

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.

Example 1

Input
n = 5
Output
2
Why
rows of 1 and 2 use 3 coins; 2 left over is not enough for row 3

Example 2

Input
n = 8
Output
3
Why
1 + 2 + 3 = 6; 2 left over

Example 3

Input
n = 1
Output
1

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

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.