AlgoMaster Logo

Inverted Pyramid

Last Updated: June 7, 2026

easy
3 min read

Understanding the Problem

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.

Key Constraints:

  • 1 <= n <= 100n 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.
  • No trailing spaces → Each row ends right after its last star. Spaces appear only on the leading side. Row i is i - 1 spaces followed by 2(n - i) + 1 stars, with nothing after.

Approach 1: Spaces Then Stars (Shrinking)

Intuition

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.

Algorithm

  1. Create an empty list to hold the rows.
  2. Loop i from 1 to n, where i is the current row number.
  3. Append i - 1 spaces to a fresh string.
  4. Append 2(n - i) + 1 stars to the same string.
  5. Add the finished string to the list.
  6. After the loop, return the list.

Example Walkthrough

Input:

3
n

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:

0
*****
1
***
2
*
result

Code

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?

Approach 2: Reverse of a Pyramid

Intuition

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.

Algorithm

  1. Create an empty list to hold the rows.
  2. Loop i from 1 to n and build the upright pyramid: n - i spaces followed by 2i - 1 stars, adding each row to the list.
  3. Reverse the list so the widest row comes first and the single star comes last.
  4. Return the reversed list.

Example Walkthrough

Input:

3
n

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:

0
*****
1
***
2
*
result

Code