Next Smallest Palindrome Problem Statement

Given a string representation of a number 'S', determine the smallest palindrome that is strictly greater than this number 'N'.

Example:

Input:
3
2
99
3
123
4
4567
Output:
101
131
4664
Explanation:

The first input '99' has the next palindrome as '101'. The second input '123' results in '131', and for '4567', it is '4664'.

Constraints:

  • 1 <= T <= 100
  • 1 <= len(S) <= 10^4

Note: The length of string 'S' indicates the number of digits in number 'N'. A single digit or multi-digit number can be a palindrome.

Ankita Pandey
1y

import java.io.*;

class GFG

{

{

int n, k, rev = 0;

n = num;

while (num != 0) {

k = num % 10;

rev = (rev * 10) + k;

num = num / 10;

}

// check if num and its reverse are same

if (n == rev) {

return 1;

}

else {

return 0;

}

}

public static void main(String[] args)

{

int num = 9687;

while (isPalindrome(num) == 0) {

num = num + 1;

}

System.out.print("Next Palindrome : ");

System.out.print(num);

}

}

Help your peers!
Add answer anonymously...
SPRINKLR Full Stack Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter