Animate the Inverted Pyramid pattern and understand how spaces grow and stars shrink by 2 per row.
About this algorithm
The inverted pyramid is the pyramid upside down: a full base of 2n - 1 stars on top, narrowing by two stars a row to a single star at the bottom centre. Each row gains one leading space as it loses a star from each side.
Do this page right after the pyramid. The grid and the centring idea are the same; what changes is that the leading spaces now grow with i and the star count shrinks. That is exactly the lower half of a diamond, so working it out here makes the diamond page a matter of joining two shapes you already know. If your shape should stay flush left while it shrinks, the inverted triangle is the simpler page.
n rows and 2n - 1 columns. Row 0 is now the widest instead of the narrowest.
Row i starts with i spaces: none on top, n - 1 at the bottom. That is the pyramid rule with i swapped for n - 1 - i.
Row i holds 2(n - i) - 1 stars: 2n - 1 on top, then two fewer each row, down to 1. In column terms the stars run from column i to column 2n - 2 - i, the same distance in from both edges.
For each column j, print a star when i <= j <= 2n - 2 - i and a space otherwise, then end the row.
At n = 5 the grid is 5 rows by 9 columns. Row 0 covers columns 0 to 8: nine stars. Row 1 has one space and covers columns 1 to 7: seven stars. Row 2 covers 2 to 6: five. Row 3 covers 3 to 5: three. Row 4 covers only column 4, a single star exactly under the middle of the top row. That is 9 + 7 + 5 + 3 + 1 = 25 stars and 20 spaces, the same totals as the pyramid, because each row here is one of the pyramid rows in reverse order.
Two quick tests catch most mistakes. At i = 0 the range must be 0 to 2n - 2, the full width. At i = n - 1 the start and end must be the same column, n - 1, the centre. If either fails, the formula has an off-by-one.
Time: n rows times 2n - 1 columns is about 2n² cell checks, each a constant amount of work. Space: only the loop counters and the two column bounds per row are kept.