Pyramids
InterviewTime limit1sMemory limit1024 MB
Given N blocks, repeatedly take out the largest triangular number that fits, and print the resulting pyramid heights in decreasing order.
- Level
Easy3 of 10
- Topics
- Greedy, Math, Implementation, Binary search
- Solved
- No attempts yet
Problem

Elena found a box full of cube-shaped blocks and decided to build the tallest pyramid she could, and to build more pyramids if any blocks were left over.
A pyramid must be stable and beautiful: if the bottom row of a pyramid has blocks, the row directly above it must have exactly blocks, and so on. A completed pyramid has exactly rows, and its top row always holds a single block. Therefore a pyramid of height uses blocks.
Given the number of blocks, repeatedly build the tallest pyramid the remaining blocks allow: build the tallest possible pyramid, then from the leftover blocks build the tallest possible pyramid again, and continue until no blocks remain. Determine the heights of the pyramids that can be built.
Input
A single integer — the number of available blocks.
Output
Print the heights of the pyramids that can be built, one per line, from the tallest to the shortest.