AlgoMaster Logo

Pyramid of Stars

Last Updated: June 7, 2026

easy
4 min read

Understanding the Problem

Each row of a pyramid has two distinct parts instead of one: a block of leading spaces that pushes the stars toward the center, then the stars themselves. The spaces are what make the shape a pyramid rather than a right-angled triangle, so they matter as much as the stars.

The pyramid has n rows and a full width of 2n - 1 characters. The top row holds a single star, and each row below adds two more stars, one on each side, while dropping one leading space. That trade keeps the stars centered as the rows widen. For row i, there are n - i leading spaces and 2i - 1 stars. With n = 3, the rows are *, ***, and *****.

Two numbers control each row, and they move in opposite directions: as i rises, the space count n - i falls while the star count 2i - 1 grows.

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. Only the leading side carries spaces. Row i is n - i spaces followed by 2i - 1 stars, with nothing after.

Approach 1: Spaces Then Stars

Intuition

Build each row in two stages that match its two parts: lay down the leading spaces, then the stars. For row i, append n - i spaces and then 2i - 1 stars.

The two counts come straight off the row number. The space count starts at n - 1 for the top row and shrinks to 0 for the bottom row, while the star count starts at 1 and grows by two each row. Append them in that order and the stars stay centered.

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 n - i spaces to a fresh string.
  4. Append 2i - 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 3 - 1 = 2 and the star count is 2(1) - 1 = 1, building " *". At i = 2, there is 1 space and 3 stars, building " ***". At i = 3, there are 0 spaces and 5 stars, building "*****". The three rows collected in order form the pyramid:

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

Code

This approach recomputes 2i - 1 from the row number on every pass. Since that count just goes 1, 3, 5, 7 and rises by two each time, can we track it with a running counter instead?

Approach 2: Track the Star Count

Intuition

The star count goes 1, 3, 5, 7, rising by two each row. Rather than evaluating 2i - 1 afresh every pass, keep a counter that starts at 1 and add 2 after each row.

The space count follows from the star count. The total width is fixed at 2n - 1, and the row uses stars characters for its stars. Since the pyramid is left-padded only, the leading spaces are (2n - 1 - stars) / 2, which equals n - i. One counter gives both pieces of the row.

Algorithm

  1. Create an empty list to hold the rows.
  2. Set a star counter stars to 1.
  3. Compute the fixed width as 2n - 1.
  4. Loop n times. On each pass, the leading space count is (width - stars) / 2.
  5. Append that many spaces, then stars stars, and add the row to the list.
  6. Add 2 to stars for the next row.
  7. After the loop, return the list.

Example Walkthrough

Input:

3
n

The width is 2(3) - 1 = 5 and stars begins at 1. On the first pass, the spaces are (5 - 1) / 2 = 2, building " *", then stars becomes 3. On the second pass, the spaces are (5 - 3) / 2 = 1, building " ***", then stars becomes 5. On the third pass, the spaces are (5 - 5) / 2 = 0, building "*****". The accumulated rows form the pyramid:

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

Code