Last Updated: June 7, 2026
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.
1 <= n <= 9 → n 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.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.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.
result to hold the rows.i from 1 to n:1, 2, ..., i to the string.i - 1, i - 2, ..., 1 to the string.result.result.Input:
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:
i writes 2i - 1 digits, so the total across all rows grows with the square of n.n² characters, since the row lengths grow linearly with the row index.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?
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".
result to hold the rows.i from 1 to n:up as 1, 2, ..., i.up without its last character and reverse it to form the descending part.up with the reversed part to make the row.result.result.Input:
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":
i characters, so summing over all rows grows with the square of n.n² characters. The temporary rising and reversed strings for a single row add only linear space at a time.