AlgoMaster Logo

Butterfly Pattern

Last Updated: June 7, 2026

medium
5 min read

Understanding the Problem

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.

Key Constraints:

  • 1 <= n <= 100n 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.
  • Width is always 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.
  • Height is always 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.

Approach 1: Two Halves

Intuition

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.

Algorithm

  1. Create an empty list to hold the rows.
  2. For 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.
  3. For i from n - 1 down to 1, build the same kind of row and add it to the list.
  4. Return the list of rows.

Example Walkthrough

Input:

3
n

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:

0
* *
1
** **
2
******
3
** **
4
* *
result

Code

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.

Approach 2: Distance From the Middle

Intuition

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.

Algorithm

  1. Create an empty list to hold the rows.
  2. Loop a row index r from 0 to 2n - 2, covering every row.
  3. Compute the distance from the middle as the absolute value of r - (n - 1).
  4. Let k = n - distance, which is the number of stars on each side for that row.
  5. Build a row with k stars, then 2 * (n - k) spaces, then k stars, and add it to the list.
  6. Return the list of rows.

Example Walkthrough

Input:

3
n

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:

0
* *
1
** **
2
******
3
** **
4
* *
result

Code