Problem 849
Arranging Coins
Easy · 10 pts · Math, Binary Search
Problem
You have N coins and want to build a staircase: the first row has 1 coin, the second 2 coins, the k-th row k coins. The last row may be incomplete. Print the number of complete rows. K complete rows need K × (K + 1) / 2 coins, so find the largest K with K × (K + 1) / 2 <= N, by binary search or from the quadratic formula with an exact integer correction.
Input
A single integer N.
Output
Print the number of complete rows.
Constraints
0 <= N <= 2^31 - 1
Example input
8
Example output
3
Explanation
With 8 coins, rows of 1, 2 and 3 coins use 6 coins, and the fourth row would need 4 more but only 2 are left. So 3 rows are complete.