The Wayback Machine - https://web.archive.org/web/20241008111347/https://www.geeksforgeeks.org/next-higher-palindromic-number-using-set-digits/
Open In App

Next higher palindromic number using the same set of digits

Last Updated : 26 Apr, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
Companies:
Show Topics
Solve Problem
Medium
23.61%
17.1K

Given a palindromic number num having n number of digits. The problem is to find the smallest palindromic number greater than num using the same set of digits as in num. If no such number can be formed then print “Not Possible”. 
The number could be very large and may or may not even fit into long long int.

Examples: 

Input : 4697557964
Output :  4756996574

Input : 543212345
Output : Not Possible

Approach:  

Follow the below steps to solve the problem:

  1. If number of digits n <= 3, then print “Not Possible” and return.
  2. Calculate mid = n/2 – 1.
  3. Start traversing from the digit at index mid up to the 1st digit and while traversing find the index i of the rightmost digit which is smaller than the digit on its right side.
  4. Now search for the smallest digit greater than the digit num[i] in the index range i+1 to mid. Let the index of this digit be smallest.
  5. If no such smallest digit found, then print “Not Possible”.
  6. Else the swap the digits at index i and smallest and also swap the digits at index n-i-1 and n-smallest-1. This step is done so as to maintain the palindromic property in num.
  7. Now reverse the digits in the index range i+1 to mid. Also If n is even then reverse the digits in the index range mid+1 to n-i-2 else if n is odd then reverse the digits in the index range mid+2 to n-i-2. This step is done so as to maintain the palindromic property in num.
  8. Print the final modified number num.

Implementation:

C





C++





Java





Python





C#





PHP





Javascript





Output

Next Palindrome: 4756996574

Time Complexity: O(n)
Auxiliary Space: O(1)



Previous Article
Next Article

Similar Reads

Next higher number with same number of set bits
Given a number x, find next number with same number of 1 bits in it's binary representation.For example, consider x = 12, whose binary representation is 1100 (excluding leading zeros on 32 bit machine). It contains two logic 1 bits. The next higher number with two logic 1 bits is 17 (100012).Algorithm:When we observe the binary sequence from 0 to 2
8 min read
Next higher number using atmost one swap operation
Given a non-negative number num. The problem is to find the smallest number greater than num by performing atmost on swap operation between any two digits in num. If no larger number can be formed then print "Not Possible".The number could be very large and may not even fit into long long int. Examples: Input : num = "218765"Output : 258761Explanat
11 min read
Minimum digits to be removed to make either all digits or alternating digits same
Given a numeric string str, the task is to find the minimum number of digits to be removed from the string such that it satisfies either of the below conditions: All the elements of the string are the same.All the elements at even position are same and all the elements at the odd position are same, which means the string is alternating with the equ
7 min read
Find next greater number with same set of digits
Given a number N as string, find the smallest number that has same set of digits as N and is greater than N. If N is the greatest possible number with its set of digits, then print "Not Possible". Examples: Input: N = "218765"Output: "251678"Explanation: The next number greater than 218765 with same set of digits is 251678. Input: n = "1234"Output:
9 min read
Longest Palindromic Substring using Palindromic Tree | Set 3
Given a string, find the longest substring which is a palindrome. For example, if the given string is “forgeeksskeegfor”, the output should be “geeksskeeg”. Prerequisite : Palindromic Tree | Longest Palindromic Substring Structure of Palindromic Tree : The palindromic Tree’s actual structure is close to the directed graph. It is actually a merged s
15+ min read
Point to next higher value node in a linked list with an arbitrary pointer
Given singly linked list with every node having an additional "arbitrary" pointer that currently points to NULL. Need to make the "arbitrary" pointer point to the next higher value node. We strongly recommend to minimize your browser and try this yourself first A Simple Solution is to traverse all nodes one by one, for every node, find the node whi
15+ min read
Javascript Program For Pointing To Next Higher Value Node In A Linked List With An Arbitrary Pointer
Given singly linked list with every node having an additional "arbitrary" pointer that currently points to NULL. Need to make the "arbitrary" pointer point to the next higher-value node. We strongly recommend to minimize your browser and try this yourself first A Simple Solution is to traverse all nodes one by one, for every node, find the node whi
4 min read
Next greater Number than N with the same quantity of digits A and B
Given a number [Tex]N [/Tex]and two digits [Tex]A [/Tex]and [Tex]B [/Tex]. The task is to find the least number not less than N which contains the equal number of digits A and B.Note: N <= 107Examples: Input : N = 4500, A = 4, B = 7 Output : 4747 The number greater than 4500 which has the same quantity of number '4' and number '7' is 4747.Input
7 min read
Find Nth even length palindromic number formed using digits X and Y
Given an integer N, the task is to find the Nth even palindromic number of even length and only comprising of the digits X and Y where X, Y > 0.Examples: Input: N = 9, X = 4, Y = 5 Output: 454454 Explanation: Even length palindromic numbers using 4 & 5 are 44, 55, 4444, 4554, 5445, 5555, 444444, 445544, 454454, ... 9th term of the above seri
8 min read
Make palindromic string non-palindromic by rearranging its letters
Given string str containing lowercase alphabets (a - z). The task is to print the string after rearranging some characters such that the string becomes non-palindromic. If it's impossible to make the string non-palindrome then print -1. Examples: Input: str = "abba" Output: aabb Input: str = "zzz" Output: -1 Brute Force: Iterate through all possibl
9 min read
Minimum cuts required to convert a palindromic string to a different palindromic string
Given palindromic string s, the task is to find minimum k, such that you can cut this string into k+1 parts, and then unite them in such a way that the final string will be a palindrome and it won't be equal to the initial string s. If it is impossible then print -1.Examples: Input : string = "civic" Output : 2 Explanation : ci | v | ic --> ic |
15+ min read
Count pairs of same parity indexed elements with same MSD after replacing each element by the sum of maximum digit * A and minimum digits * B
Given an array arr[] of N 3-digit integers and two integers a and b, the task is to modify each array element according to the following rules: Find the maximum, say M, and minimum digit, say m, of each array element arr[i].Update the array element arr[i] as (A * M + B * m). The task is to count the number of pairs such that the chosen elements are
12 min read
Count of digits in N and M that are same and are present on the same indices
Given two number N and M, the task is to find the count of digits in N and M that are the same and are present on the same indices. Examples: Input: N = 123, M = 321Output: 1Explanation: Digit 2 satisfies the condition Input: N = 123, M = 111Output: 1 Approach: The problem can be solved using two-pointer approach. Convert N and M for ease of traver
4 min read
Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
Given three positive integers N, A, and B. The task is to count the numbers of length N containing only digits A and B and whose sum of digits also contains the digits A and B only. Print the answer modulo 109 + 7.Examples: Input: N = 3, A = 1, B = 3 Output: 1 Possible numbers of length 3 are 113, 131, 111, 333, 311, 331 and so on... But only 111 i
15 min read
Rearrange given Array such that all set bit positions have higher value than others
Given an array B1[] and a binary array B2[] each of size N, the task is to rearrange array B1[] in a way such that for all setbit position i of B2[] the value of B1[i] will be greater than the values where bit is not set in B2[] i.e for all (i, j) B1[i] > B1[j] when B2[i] = 1, B2[j] = 0 (0 â‰Ī i, j < N). Note: If multiple arrangements are possi
13 min read
Largest palindromic number by permuting digits
Given N(very large), the task is to print the largest palindromic number obtained by permuting the digits of N. If it is not possible to make a palindromic number, then print an appropriate message. Examples : Input : 313551 Output : 531135 Explanations : 531135 is the largest number which is a palindrome, 135531, 315513 and other numbers can also
12 min read
Closest (or Next) smaller and greater numbers with same number of set bits
Given a positive integer n, print the next smallest and the previous largest number that has the same number of 1 bit in their binary representation. Examples: Input : n = 5 Output : Closest Greater = 6 Closest Smaller = 3 Note that 5, 6 and 3 have same number of set bits. Input : n = 11 Output : Closest Greater = 13 Closest Smaller = 7 The Brute F
15+ min read
Binary representation of next greater number with same number of 1's and 0's
Given a binary input that represents binary representation of positive number n, find binary representation of smallest number greater than n with same number of 1's and 0's as in binary representation of n. If no such number can be formed, print "no greater number".The binary input may be and may not fit even in unsigned long long int. Examples: I
12 min read
Find Next number having distinct digits from the given number N
Given a natural number N, the task is to find the next number having distinct digits from the given number.Examples: Input: N = 19 Output: 20 Explanation: Next number to 19 whose digits are different from 19 is 20.Input: N = 2019 Output: 3333 Explanation: Next number to 2019 whose digits are different from 2019 is 3333. Approach: The idea is to use
10 min read
Sum of all N digit palindromic numbers divisible by 9 formed using digits 1 to 9
Given a number N, the task is to find the sum of all N digits palindromic numbers (formed by digits from 1 to 9) that are divisible by 9. Examples: Input: N = 1 Output: 9 Explanation: Only 9 is a palindromic number of 1 digit divisible by 9 Input: N = 3 Output: 4995 Explanation: Three-digit Palindromic Numbers divisible by 9 are - 171, 252, 333, 41
6 min read
Lexicographically next greater string using same character set
Given a number K and a string S, We have to Find the lexicographically smallest string str of length K such that it's set of letters is a subset of the set of letters of S and S is lexicographically smaller than str. Examples: Input :k = 3 s = zbf Output: zbz Explanation: zbz is greater than zbf and it is smaller than any other lexicographically gr
8 min read
Find largest number smaller than N with same set of digits
Given a number N in the form of string. The task is to find the greatest number that has same set of digits as N and is smaller than N. If it is not possible to find any such number, then print "not possible". Examples: Input: N = "218765" Output: 218756 Input: N = "1234" Output: Not Possible Input: N = "262345" Output: 256432Recommended: Please so
9 min read
Find smallest possible Number from a given large Number with same count of digits
Given a number K of length N, the task is to find the smallest possible number that can be formed from K of N digits by swapping the digits any number of times. Examples: Input: N = 15, K = 325343273113434 Output: 112233333344457 Explanation: The smallest number possible after swapping the digits of the given number is 112233333344457 Input: N = 7,
5 min read
Count of integers in a range which have even number of odd digits and odd number of even digits
Given a range [L, R], the task is to count the numbers which have even number of odd digits and odd number of even digits. For example, 8 has 1 even digit and 0 odd digit - Satisfies the condition since 1 is odd and 0 is even.545 has 1 even digit and 2 odd digits - Satisfies the condition since 1 is odd and 2 is even.4834 has 3 even digits and 1 od
11 min read
Find smallest number with given number of digits and sum of digits under given constraints
Given two integers S and D, the task is to find the number having D number of digits and the sum of its digits as S such that the difference between the maximum and the minimum digit in the number is as minimum as possible. If multiple such numbers are possible, print the smallest number.Examples: Input: S = 25, D = 4 Output: 6667 The difference be
7 min read
Number formed by deleting digits such that sum of the digits becomes even and the number odd
Given a non-negative number N, the task is to convert the number by deleting some digits of the number, such that the sum of the digits becomes even but the number is odd. In case there is no possible number, then print -1.Note: There can be multiple numbers possible for a given N.Examples: Input: N = 18720 Output: 17 Explanation: After Deleting 8,
5 min read
Find second smallest number from sum of digits and number of digits
Given the sum of digits as S and the number of digits as D, the task is to find the second smallest number Examples: Input: S = 9, D = 2Output: 27Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Input: S = 16, D = 3Output: 178Explanation: 169 is the smallest number possible with s
8 min read
Number of digits in the nth number made of given four digits
Find the number of digits in the nth number constructed by using 6, 1, 4, and 9 as the only digits in the ascending order. First few numbers constructed by using only 6, 1, 4, and 9 as digits in ascending order would be: 1, 6, 4, 9, 11, 14, 16, 19, 41, 44, 46, 49, 61, 64, 66, 69, 91, 94, 96, 99, 111, 114, 116, 119 and so on. Examples: Input : 6Outp
13 min read
Find smallest number with given number of digits and sum of digits
How to find the smallest number with given digit sum s and number of digits d? Examples : Input : s = 9, d = 2 Output : 18 There are many other possible numbers like 45, 54, 90, etc with sum of digits as 9 and number of digits as 2. The smallest of them is 18. Input : s = 20, d = 3 Output : 299 Recommended PracticeSmallest numberTry It! A Simple So
9 min read
Find the Largest number with given number of digits and sum of digits
Given an integer s and d, The task is to find the largest number with given digit sum s and the number of digits d. Examples: Input: s = 9, d = 2Output: 90 Input: s = 20, d = 3Output: 992 Recommended PracticeLargest number possibleTry It! Naive Approach: Consider all m digit numbers and keep a max variable to store the maximum number with m digits
13 min read