Last Updated: June 7, 2026
The hourglass is two pyramids joined at their tips. The top is an inverted pyramid, wide at the top and narrowing to a single star. The bottom is a normal pyramid, starting from a single star and widening back out. Where they meet, in the middle row, the pattern pinches down to one star, the waist of the shape.
Each row needs two counts lined up: the leading spaces that push the stars toward the center, and the stars themselves. Wrong spaces and the stars no longer center; wrong star count and the edges look ragged.
For a top row numbered i from 1 to n, the row begins with i - 1 spaces, so the first row has none and each row below is indented one more. After the spaces come 2 * (n - i) + 1 stars, widest at the top and shrinking by two each row until a single star at i = n. There are no trailing spaces. The bottom half then replays rows n - 1 down to 1, mirroring the top so the lower pyramid opens back up.
1 <= n <= 100 → n is always at least 1. The smallest case, n = 1, gives a single row *, which is the pinch point with no pyramid above or below it.2 * (n - i) + 1 stars, which is always an odd number. That odd count is what lets the stars sit symmetrically around a center column.n - 1 leading spaces before its single star.The hourglass divides into a top inverted pyramid and a bottom upright pyramid. Handle each half with its own loop, sharing one rule for building a row from a value i: i - 1 spaces followed by 2 * (n - i) + 1 stars.
The top loop walks i upward from 1 to n, so the indentation increases and the star count shrinks down to a single star. The bottom loop walks i back down from n - 1 to 1, so the indentation decreases and the stars widen again.
i from 1 up to n, build a row with i - 1 leading spaces followed by 2 * (n - i) + 1 stars, and add it to the list.i from n - 1 down to 1, build the same kind of row and add it to the list.Input:
The top loop runs for i = 1, 2, 3. At i = 1, the row is 0 spaces and 2 * (3 - 1) + 1 = 5 stars, giving *****. At i = 2, it is 1 space and 2 * (3 - 2) + 1 = 3 stars, giving ***. At i = 3, it is 2 spaces and a single star, giving *, the pinch point. The bottom loop then runs for i = 2, 1, producing *** and ***** again. The rows stack into the full hourglass:
2n - 1 rows, and each row takes up to 2n - 1 work to build its spaces and stars, so the total work grows with the square of n.2n - 1 rows of up to 2n - 1 characters each, which is on the order of n^2 characters in total.Two loops keep the row rule readable, but they repeat the same logic in both directions. The whole hourglass can be drawn in one pass if we describe each row by its distance from the pinched middle.
The hourglass is symmetric around its middle row, the single-star pinch point. A row's width depends only on how far it sits from that middle, not on whether it is above or below.
Number all 2n - 1 rows with a single index and measure the distance from the middle. At the middle the distance is 0, so there is one star and n - 1 leading spaces. As the distance grows toward n - 1 at the edges, the spaces drop to 0 and the stars grow. The leading spaces equal (n - 1) - distance, and the star count equals 2 * distance + 1. One loop over all the rows is enough.
r from 0 to 2n - 2, covering every row.r - (n - 1).(n - 1) - distance and the star count to 2 * distance + 1.Input:
With n = 3, the middle row index is n - 1 = 2, and there are 2n - 1 = 5 rows. At r = 0, the distance is |0 - 2| = 2, so the row has (3 - 1) - 2 = 0 spaces and 2 * 2 + 1 = 5 stars, giving *****. At r = 1, the distance is 1, so 1 space and 3 stars, giving ***. At r = 2, the distance is 0, so 2 spaces and 1 star, giving *, the pinch. At r = 3, the distance climbs back to 1, giving ***. At r = 4, the distance is 2, giving *****. The single loop produces the same five rows:
2n - 1 times, and building each row costs up to 2n - 1 work, so the total is on the order of n^2.2n - 1 rows, each up to 2n - 1 characters wide, which adds up to roughly n^2 characters.