Last Updated: June 7, 2026
Under the decorative shape is a rule about counts of stars and spaces that changes from one row to the next.
The shape is symmetric in two ways. Top to bottom, the rows above the middle mirror the rows below it. Left to right, each row has the same number of stars on its left as on its right, with a gap in between. The widest row sits in the middle, where the gap closes completely and the row becomes solid stars.
For an upper row numbered i from 1 to n, the row begins with i stars and ends with i stars, with 2 * (n - i) spaces between them. When i is small the wings are thin and the gap is wide. When i reaches n, the gap shrinks to zero and the two groups meet, giving a full row of 2n stars. The lower half then walks back down, repeating rows n - 1 through 1.
1 <= n <= 100 → n is always at least 1, so there is always something to draw. The smallest case, n = 1, gives a single row **, which is one star on each side with no gap.2n → Every row, including the wide middle row, has the same total length of 2n characters. The stars and spaces in any row must add up to exactly that width.2n - 1 → The top half has n rows, and the bottom half repeats n - 1 of them, so the full pattern has 2n - 1 rows in total.Handle each half with its own loop and reuse the same row-building rule for both. Every row driven by i is i stars, then 2 * (n - i) spaces, then i more stars. The top loop walks i upward from 1 to n, growing the wings until they meet. The bottom loop walks i back down from n - 1 to 1, shrinking them again.
i from 1 up to n, build a row with i stars, then 2 * (n - i) spaces, then i stars, and add it to the list.i from n - 1 down to 1, build the same kind of row and add it to the list.Input:
The top loop runs for i = 1, 2, 3. At i = 1, the row is 1 star, 2 * (3 - 1) = 4 spaces, then 1 star, giving * *. At i = 2, it is 2 stars, 2 * (3 - 2) = 2 spaces, then 2 stars, giving ** **. At i = 3, it is 3 stars, 0 spaces, then 3 stars, giving the solid middle row ******. The bottom loop then runs for i = 2, 1, producing ** ** and * * again. The rows stack into the full butterfly:
2n - 1 rows, and each row takes up to 2n work to build its characters, so the total work grows with the square of n.2n - 1 rows of width 2n, which is on the order of n^2 characters in total.Two loops keep the row rule simple but write the same logic twice. The whole shape can be drawn in a single pass by describing each row by how far it sits from the middle.
Each row's width depends only on its distance from the middle row, the same whether the row is above or below. That is why the pattern is symmetric.
Number all 2n - 1 rows with a single index and measure the distance from the middle. Let k = n - distanceFromMiddle. At the middle the distance is 0, so k = n and the row is solid. At the top and bottom edges the distance reaches n - 1, so k shrinks to 1, giving the thin one-star wings. Each row then has k stars, 2 * (n - k) spaces, and k stars, the same rule driven by one loop.
r from 0 to 2n - 2, covering every row.r - (n - 1).k = n - distance, which is the number of stars on each side for that row.k stars, then 2 * (n - k) spaces, then k stars, and add it to the list.Input:
With n = 3, the middle row index is n - 1 = 2, and there are 2n - 1 = 5 rows. At r = 0, the distance is |0 - 2| = 2, so k = 3 - 2 = 1, giving 1 star, 4 spaces, 1 star, or * *. At r = 1, the distance is 1, so k = 2, giving ** **. At r = 2, the distance is 0, so k = 3, giving the solid ******. At r = 3, the distance climbs back to 1 and k = 2, giving ** **. At r = 4, the distance is 2 and k = 1, giving * *. The single loop produces the same five rows:
2n - 1 times, and building each row costs up to 2n work, so the total is on the order of n^2.2n - 1 rows, each 2n characters wide, which adds up to roughly n^2 characters.