Find the maximum possible height of a triangle built from a limited number of blocks, where each lower row must contain one more block than the row above it.
Maximum Height of a Triangle
You are given an integer n, representing the number of identical blocks available.
Using these blocks, you want to build a triangle with rows stacked from top to bottom:
- the top row contains
1block, - the next row contains
2blocks, - the next row contains
3blocks, - and so on.
A triangle of height h uses exactly 1 + 2 + ... + h blocks.
Return the maximum height of a triangle that can be built using at most n blocks.
Notes
- You do not need to use all blocks.
- The triangle height is the number of rows.
- If
n = 0, the answer is0.
Input Format
- A single integer
n.
Output Format
- Return a single integer: the maximum feasible triangle height.
Constraints
- The answer fits in a standard integer type for the chosen platform.
Example 1
Input
n = 6
Output
3
Explanation
A triangle of height 3 uses 1 + 2 + 3 = 6 blocks, which fits exactly.
Example 2
Input
n = 7
Output
3
Explanation
Height 4 would need 1 + 2 + 3 + 4 = 10 blocks, which is too many. So the maximum height is 3.
Show 1 more example
Example 3
Input
n = 0
Output
0
Explanation
No blocks means no rows can be built.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.