A2Z Lesson

Log in
← Back to the A2Z sheet

Step 01 Beginner Problems — Patterns

Pattern 12 — Number Crown Pattern

Easy

Two number triangles facing each other across a closing gap: the left side counts up 1, 2, 3, ... while the right side mirrors it back down, and in the last row the two sides meet to form the palindrome 1 2 3 4 4 3 2 1. It is the classic number crown — a lesson in splitting a row into a left part, a gap, and a right part.

1             1
1 2         2 1
1 2 3     3 2 1
1 2 3 4 4 3 2 1
Output for n = 4

How to think about it

Use 1-based rows r = 1, 2, ..., n. Row r has three parts: a left run of the numbers 1..r, a middle gap of 2(n - r) empty slots, and a right run of the numbers r..1 in decreasing order. The total slots per row are r + 2(n - r) + r = 2n — constant for every row — which is why all rows have the same printed width. As r grows by 1, each side gains one number and the gap loses two slots, so the sides march toward each other until at r = n the gap vanishes and the row reads 1..n followed by n..1.

Code

The code builds the left list [1..r], a gap list of 2(n - r) single-space placeholders, and the right list [r..1], then joins everything with spaces. Using a blank string as a placeholder token keeps every slot the same width, so the right side stays perfectly aligned as the gap shrinks.

def pattern(n): for i in range(n): r = i + 1 left = [str(x) for x in range(1, r + 1)] gap = [" "] * (2 * (n - r)) right = [str(x) for x in range(r, 0, -1)] print(" ".join(left + gap + right))

Key points

  • Row r = left run (1..r) + gap of 2(n - r) slots + right run (r..1).
  • Every row occupies exactly 2n slots — constant width is the alignment guarantee.
  • Each step down: the sides grow by 1 each and the gap shrinks by 2.
  • The final row is a palindrome: 1 2 ... n n ... 2 1.
  • Placeholder tokens (' ') keep spacing uniform; printing raw spaces inline is where alignment usually breaks.

Suggested questions

Pattern 19 — Symmetric-Void Pattern The star version of the same idea — two blocks and a managed void on a fixed canvas.
Pattern 11 — Binary Number Triangle Pattern Another triangle generated by a single positional rule rather than stored data.
Pattern 22 — Number Pattern Concentric number layers — values decided by position, like the crown's two sides.
Palindrome Number ↗ The crown's last row is a palindrome; this problem checks the same property in a number.

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.