today Question

Next Smallest Palindrome

Difficulty: Hard

Given a number, in the form of an array num[] containing digits from 1 to 9(inclusive). Find the next smallest palindrome strictly larger than the given number.

Screenshot 2026-04-13 at 6.26.26 AM.png


Answers :::

Intuition

The core idea is that a palindrome is determined by its left half. Since we want the smallest number larger than the current one, we should try to change the number as little as possible. The most significant digits are on the left, so we try to keep the left half the same and "mirror" it to the right.


Approach

We can break the logic into three main steps:

Step A: Handle the "All 9s" Case

If the input is 999, the next palindrome isn't just a modification of these digits; it adds a digit: 1001.

Step B: The Mirror Test

Compare the original right half with the mirrored left half to see if mirroring alone is enough.

  1. Find the first pair of digits (moving from the center outwards) that are different.
  2. If the digit on the left side is greater than the one on the right, mirroring the left half will result in a larger number.