Skip to main content
Back to problems
Leetcode
Medium
Arrays
Math
Greedy
Google
Maximum Height Of A Triangle

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.

Acceptance 0%
Problem Statement

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 1 block,
  • the next row contains 2 blocks,
  • the next row contains 3 blocks,
  • 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 is 0.

Input Format

  • A single integer n.

Output Format

  • Return a single integer: the maximum feasible triangle height.

Constraints

  • 0n0 \le n
  • The answer fits in a standard integer type for the chosen platform.
Examples
Sample cases returned by the problem API.

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.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.