Last Updated: June 7, 2026
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.
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 n - i spaces followed by 2i - 1 stars, with nothing after.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.
i from 1 to n, where i is the current row number.n - i spaces to a fresh string.2i - 1 stars to the same string.Input:
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:
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.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?
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.
stars to 1.2n - 1.n times. On each pass, the leading space count is (width - stars) / 2.stars stars, and add the row to the list.2 to stars for the next row.Input:
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:
n times, and building each row still writes 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.