AlgoMaster Logo

Mirror Number Pattern

Last Updated: June 7, 2026

easy
4 min read

Understanding the Problem

A mirror number pattern stacks rows that climb to a peak and then slide back down. Row 1 is "1". Row 2 climbs 1, 2 and falls back to 1, giving "121". Row 3 climbs to 3 and back through 2, 1, giving "12321". Each row reads the same from either end, a numeric palindrome.

The output is a list of strings, one per row. Row i counts up 1, 2, ..., i and then down i - 1, i - 2, ..., 1. The peak digit i appears once in the middle, and every digit below it appears twice. For n = 3 the answer is ["1", "121", "12321"].

Because the digits sit right next to each other, n is capped so each value is a single character. That keeps a row like "12321" reading as the digits 1, 2, 3, 2, 1 rather than a larger number.

Key Constraints:

  • 1 <= n <= 9n is always at least 1, so there is at least one row. The cap at 9 keeps every value a single digit, so the row is a clean palindrome of single characters.
  • Row i goes up to i and back down → Row i has i digits on the way up and i - 1 on the way down, for a total length of 2i - 1 characters. The peak digit i sits alone in the center.

Approach 1: Up Then Down

Intuition

Each row is two counts joined together. Count up from 1 to the peak i, then count down from i - 1 back to 1. Writing those one after the other gives the full palindrome for that row.

The going-down half starts at i - 1, not i. The peak digit i was already written as the last step of the going-up half, so starting the descent at i - 1 avoids repeating the peak. That keeps the center of the row a lone digit instead of a doubled one.

Algorithm

  1. Create an empty list result to hold the rows.
  2. For each row index i from 1 to n:
    • Start with an empty string for the row.
    • Append the digits 1, 2, ..., i to the string.
    • Append the digits i - 1, i - 2, ..., 1 to the string.
    • Add the finished string to result.
  3. Return result.

Example Walkthrough

Input:

3
n

For row 1 the up half is "1" and the down half is empty, so the row is "1". For row 2 the up half is "12" and the down half counts from 1, giving "121". For row 3 the up half is "123", and the down half counts 2 then 1, producing "12321". Each row joins its two halves and is collected in turn:

0
1
1
121
2
12321
result

Code

The two loops build the up and down halves separately, but the down half is really just the up half played in reverse. Can we build the rising part once and mirror it instead of counting down by hand?

Approach 2: Build and Mirror

Intuition

Since every mirror row is a palindrome, its second half is its first half reversed. The rising part 1, 2, ..., i already holds every digit the row needs. To get the falling part, take that rising string, drop its last character (the peak, which should not repeat), and reverse what remains.

Build the ascending string once, then append its reflection. The reflection produces i - 1, i - 2, ..., 1 without a second counting loop, because reversing "123" without its last digit gives "21".

Algorithm

  1. Create an empty list result to hold the rows.
  2. For each row index i from 1 to n:
    • Build the ascending string up as 1, 2, ..., i.
    • Take up without its last character and reverse it to form the descending part.
    • Join up with the reversed part to make the row.
    • Add the row to result.
  3. Return result.

Example Walkthrough

Input:

3
n

For row 1 the ascending string is "1". Dropping its last character leaves an empty string, so the row stays "1". For row 2 the ascending string is "12". Removing the peak 2 leaves "1", and reversing it is still "1", so the row is "12" + "1" = "121". For row 3 the ascending string is "123". Dropping the 3 leaves "12", which reversed is "21", giving "123" + "21" = "12321":

0
1
1
121
2
12321
result

Code