Last Updated: June 7, 2026
This is the centered pyramid turned upside down. The full-width base sits at the top, and the single star that normally crowns the pyramid is at the bottom. The stars stay centered the whole way down, so the spaces still push each row inward, in growing amounts as the rows narrow.
The inverted pyramid has n rows and a fixed width of 2n - 1 characters. Row i has i - 1 leading spaces and 2(n - i) + 1 stars, so each row below the top loses two stars and gains one leading space. With n = 3, the rows are *****, ***, and *, a triangle pointing downward.
The two counts move opposite to the upright pyramid: the spaces start at 0 on the top row and climb to n - 1 on the bottom, while the stars start at 2n - 1 and shrink to 1.
1 <= n <= 100 → n is always at least 1, so the result has at least one row. The widest row is 2n - 1 characters, at most 199, which stays small.i is i - 1 spaces followed by 2(n - i) + 1 stars, with nothing after.Build each row in two stages, leading spaces first and then stars. For row i, append i - 1 spaces, then 2(n - i) + 1 stars. Appended in that order, the stars stay centered as the pyramid narrows downward.
i from 1 to n, where i is the current row number.i - 1 spaces to a fresh string.2(n - i) + 1 stars to the same string.Input:
At i = 1, the space count is 1 - 1 = 0 and the star count is 2(3 - 1) + 1 = 5, building "*****". At i = 2, there is 1 space and 3 stars, building " ***". At i = 3, there are 2 spaces and 1 star, building " *". The three rows collected in order form the inverted pyramid:
n times, and each row appends up to 2n - 1 characters, so the total work grows with the square of n.n rows, each up to 2n - 1 characters wide, so the result grows with the square of n.The two opposing formulas take some care to keep straight. Since this shape is just an upright pyramid flipped over, can we build the upright version with its simpler counts and reverse it?
The inverted pyramid and the upright pyramid hold the same rows in opposite order. The upright lists them from the single star up to the base as *, ***, *****; the inverted lists them from the base down as *****, ***, *. Flipping one top to bottom gives the other.
So build the upright pyramid first, where row i has n - i leading spaces and 2i - 1 stars, then reverse the list. The reversal turns the widening shape into a narrowing one without changing any individual row, reusing the simpler upright formulas.
i from 1 to n and build the upright pyramid: n - i spaces followed by 2i - 1 stars, adding each row to the list.Input:
The first loop builds the upright pyramid, producing [" *", " ***", "*****"], which widens from the single star at the top to the full base at the bottom. Reversing this list swaps the top and bottom rows, leaving the full-width base on top and the single star at the bottom:
2n - 1 characters per row across n rows, and the reverse step only touches n rows, so the build step dominates and grows with the square of n.n rows, each up to 2n - 1 characters wide, so the result grows with the square of n.