Arranging Coins - Problem

You have n coins and you want to build a staircase with these coins. The staircase consists of k rows where the i-th row has exactly i coins.

The last row of the staircase may be incomplete.

Given the integer n, return the number of complete rows of the staircase you will build.

Input & Output

Example 1 — Basic Case
$ Input: n = 5
Output: 2
💡 Note: Row 1: 1 coin (4 left), Row 2: 2 coins (2 left), Row 3 needs 3 coins but only 2 left. So 2 complete rows.
Example 2 — Perfect Triangle
$ Input: n = 3
Output: 2
💡 Note: Row 1: 1 coin (2 left), Row 2: 2 coins (0 left). Exactly 2 complete rows with no coins remaining.
Example 3 — Large Number
$ Input: n = 8
Output: 3
💡 Note: Row 1: 1 coin, Row 2: 2 coins, Row 3: 3 coins (total 6), Row 4 needs 4 but only 2 left. So 3 complete rows.

Constraints

  • 1 ≤ n ≤ 231 - 1

Visualization

Tap to expand
Arranging Coins: Building StaircaseInput: n = 5 coinsArrange in staircase:Row 1: 1 coin ✓Row 2: 2 coins ✓Row 3: Need 3, have 2Incomplete!Complete rows: 2
Understanding the Visualization
1
Input
Given n=5 coins to arrange
2
Build Staircase
Row 1: 1 coin, Row 2: 2 coins, Row 3 incomplete
3
Count Complete
Return 2 (number of complete rows)
Key Takeaway
🎯 Key Insight: Find largest k where triangular number k*(k+1)/2 ≤ n
Asked in
Google 15 Amazon 12 Microsoft 8
111.0K Views
Medium Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen