Live Batches
Masterclasses
Menu
Free Courses
Account
Login / Sign Up

Next Smallest Palindrome

Hard Acceptance 19.63% Points 40.00

Given a number, in the form of an array Num[] of size N containing digits from 1 to 9(inclusive). The task is to find the next smallest palindrome strictly larger than the given number.

Examples
Example 1

Example 1

Input: N = 11, Num[] = {9, 4, 1, 8, 7, 9, 7, 8, 3, 2, 2}

Output: 9 4 1 8 8 0 8 8 1 4 9

Explanation: Next smallest palindrome is 9 4 1 8 8 0 8 8 1 4 9

Example 2

Example 2

Input: N = 5, Num[] = {2, 3, 5, 4, 5}

Output: 2 3 6 3 2

Explanation: Next smallest palindrome is 2 3 6 3 2

Hints
Hint 1
Expected Time Complexity: O(N)
Hint 2
Expected Auxiliary Space: O(1)
Constraints
  • 1 <= N <= 10^5
  • 1 <= Num[i] <= 9
Companies
Amazon Microsoft Adobe Flipkart
Topics
Array
Solution.cs C#JavaPythonC++Javascript

Unlock the code editor

Sign in to write, run, and submit your solution against the full test suite.

  • Run code against sample & hidden test cases
  • Save submissions and track your streak
  • Compare with editorial & community solutions