A2Z Lesson

Log in
← Back to the A2Z sheet

Step 01 Beginner Problems — Patterns

Pattern 20 — Symmetry Pattern

Medium

The mirror journey of Pattern 19: two star triangles start as far apart as possible — single stars at the far edges — and grow toward each other row by row until they merge into a solid band of 2n stars at the center, then separate again. The figure is symmetric across both axes, like an hourglass turned on its side.

*                 *
* *             * *
* * *         * * *
* * * *     * * * *
* * * * * * * * * *
* * * * * * * * * *
* * * *     * * * *
* * *         * * *
* *             * *
*                 *
Output for n = 5

How to think about it

Use a single row counter k = 0, 1, ..., 2n - 1 (2n rows). Let s = min(k, 2n - 1 - k) + 1 — the distance from the nearer end, plus one. Row k has s stars on the left, a gap of 2(n - s) blank slots, and s stars on the right, always filling the same 2n-slot canvas. At the ends (k = 0 and k = 2n - 1) s = 1: lone stars at the edges with the maximum void of 2n - 2 slots. In the middle (k = n - 1 and k = n) s = n: the gap closes completely and the row is solid. The min() folds the second half onto the first, so one formula generates the whole figure.

Code

One loop over 2n rows computes side_count from the folded index min(k, 2n - 1 - k) + 1, then joins side stars, gap placeholders, and side stars. Folding the index with min() is the key move — it makes the bottom half automatically mirror the top without a second loop.

def pattern(n): for k in range(2 * n): side_count = min(k, 2 * n - 1 - k) + 1 side = ["*"] * side_count gap = [" "] * (2 * (n - side_count)) print(" ".join(side + gap + side))

Key points

  • Row k: side = min(k, 2n - 1 - k) + 1 stars, gap = 2(n - side) slots — one formula, whole figure.
  • 2n rows; the solid 2n-star row appears twice at the center.
  • Index folding with min() replaces a second, mirrored loop.
  • It is Pattern 19 run backwards: the void closes, then reopens.
  • Every row spans exactly 2n slots — check the first and last rows for equal width.

Suggested questions

Pattern 19 — Symmetric-Void Pattern The same canvas with the motion reversed — the void opens first, then closes.
Pattern 9 — Diamond Star Pattern Another fully symmetric figure assembled from mirrored halves.
Pattern 10 — Half Diamond Star Pattern The rise-and-fall count min(i + 1, 2n - 1 - i) is the same folding trick in one dimension.
Palindrome Number ↗ Figures and numbers that read the same in both directions — symmetry as a checkable property.

Source note: this lesson follows the structure of Striver's public A2Z DSA course (takeUforward). Each problem links to its original platform, and all problems and content belong to their respective owners — STUDY-BUDDY does not claim ownership of the sheet or its problems, and no permission beyond public linking is claimed.