You have n coins and want to build a staircase where the kth row contains exactly k coins. The last row may be incomplete. Return the number of complete rows you can build.
Examples:
Input: n = 5
Output: 2
Explanation: Row 1 uses 1 coin, row 2 uses 2 coins. Total used: 3. Row 3 would need 3 more coins but only 2 remain, so it is incomplete.
Input: n = 8
Output: 3
Explanation: Row 1 uses 1, row 2 uses 2, row 3 uses 3. Total used: 6. Row 4 would need 4 but only 2 remain.
Input: n = 1
Output: 1
Explanation: Only row 1 is completed with 1 coin.
Input: n = 0
Output: 0
Explanation: No coins, no complete rows.
Input: n = 10
Output: 4
Explanation: Rows 1+2+3+4 = 10 coins exactly. All 4 rows are complete.
Apex Code Editor
Welcome to Lightning Challenge!
How It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Contest Alert
🏆 #CodeEveryDay September 2026 (Sep 16–30)Sponsored by FLXBL
Contest runs September 16 - 30. Complete challenges to climb the leaderboard!
Prizes 1 & 2: Salesforce certification exam voucher — won by random drawing, where every point is a ticket. see all prizes
Only the 15 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.