AlgoMaster Logo

Diamond Pattern

Last Updated: June 7, 2026

easy
4 min read

Understanding the Problem

A diamond is two pyramids stacked back to back. The top pyramid widens as it goes down, and the bottom pyramid is the same rows in reverse, narrowing back to a point. Together they form a symmetric figure 2n - 1 rows tall and, at its widest middle row, 2n - 1 characters across.

The building block is the pyramid row. For row k, counting from 1 at the tip to n at the base, the line carries n - k leading spaces, then 2k - 1 stars. As k grows, the leading spaces shrink by one and the stars grow by two, keeping every row centered while the star count climbs through the odd numbers 1, 3, 5, and so on.

For n = 4, the top pyramid is *, ***, *****, *******. The bottom half then repeats rows three, two, and one, giving *****, ***, and *. There are no trailing spaces, so each row ends right after its last star.

Key Constraints:

  • 1 <= n <= 100n is always at least 1. When n is 1 the diamond is a single * with no surrounding rows.
  • No trailing spaces → Each line stops after its last star, so the rows have different lengths and only the middle row reaches the full width of 2n - 1.
  • Total rows are 2n - 1 → The top half contributes n rows and the bottom half contributes n - 1 more, since the widest middle row is not repeated.

Approach 1: Two Halves

Intuition

Build the diamond in two passes. The first walks k from 1 to n, adding a row of n - k spaces and 2k - 1 stars each time, covering the tip down through the widest middle row.

The second pass walks k back down from n - 1 to 1. The same formula reproduces the upper rows in reverse, the mirror image. Starting at n - 1 leaves the middle row out of the second pass so it appears only once.

Algorithm

  1. Create an empty list to hold the rows.
  2. For the top half, loop k from 1 to n. Build a row of n - k spaces followed by 2k - 1 stars and add it.
  3. For the bottom half, loop k from n - 1 down to 1. Build the same kind of row and add it.
  4. Return the list once both halves are added.

Example Walkthrough

Input:

4
n

With n = 4, the top pass runs k from 1 to 4. At k = 1 the row is 3 spaces and 1 star, *. At k = 2 it is 2 spaces and 3 stars, ***. At k = 3 it is *****, and at k = 4 it is ******* with no leading space and seven stars. The bottom pass runs k from 3 down to 1, reproducing *****, ***, and *. Joining both halves gives the full diamond:

0
*
1
***
2
*****
3
*******
4
*****
5
***
6
*
result

Code

Writing the two halves separately repeats the same row-building formula twice. A single loop can cover the whole diamond by reasoning about how far each row sits from the middle.

Approach 2: Distance From the Middle

Intuition

Each row's shape is decided by how far it sits from the middle row. The middle row is row n, out of 2n - 1 rows. A row d steps from the middle, in either direction, has d leading spaces and 2 × (n - d) - 1 stars. The middle row has d = 0, so no spaces and the full 2n - 1 stars, and the two tips are d = n - 1 away with one star each.

This collapses the figure into one loop. Walk a row index r from 1 to 2n - 1, compute d as the absolute difference between r and n, then build the row from d and n. Since d is symmetric around the middle, both halves come out of the same formula.

Algorithm

  1. Create an empty list to hold the rows.
  2. Loop r from 1 to 2n - 1.
  3. Compute d, the absolute difference between r and n, which is the distance from the middle row.
  4. Build a row of d spaces followed by 2 × (n - d) - 1 stars.
  5. Add the row to the list, then return it after the loop.

Example Walkthrough

Input:

4
n

With n = 4, the middle is row 4, and r runs from 1 to 7. At r = 1, the distance is d = 3, so the row has 3 spaces and 2 × (4 - 3) - 1 = 1 star, giving *. At r = 2, d = 2 produces ***. At r = 3, d = 1 produces *****. At r = 4, d = 0 produces *******. Past the middle the distances climb again: r = 5 has d = 1, r = 6 has d = 2, and r = 7 has d = 3, mirroring the top rows. The single loop builds the whole diamond:

0
*
1
***
2
*****
3
*******
4
*****
5
***
6
*
result

Code