Given two integers A and B. The task is to find the nearest greater value to B by interchanging the digits of A. If no such permutataion possible then print -1.
Input: A = 459, B = 500
549 is the nearest greater.
Input: A = 321, B = 567
Input: A = 231, B = 125
Prerequisites: All permutations of a string
- Set the minimum value of min1 by using the Integer.MAX_VALUE
- Interchange the digit of A by using above mentioned permutation method.
- Check if the permutataion of A is less than min1 or not. If less then update min1 as A.
- Repeat this for all permutataions of A and find the minimum greater value
Below is the implementation of the above approach :
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Largest number not greater than N all the digits of which are odd
- Next greater Number than N with the same quantity of digits A and B
- Find next greater number with same set of digits
- Smallest number greater than or equal to N using only digits 1 to K
- Next greater number on the basis of precedence of digits
- Smallest number greater than or equal to X whose sum of digits is divisible by Y
- Largest number not greater than N which can become prime after rearranging its digits
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Nearest prime less than given number n
- Round the given number to nearest multiple of 10 | Set-2
- Round the given number to nearest multiple of 10
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Find smallest number with given number of digits and sum of digits under given constraints
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Find smallest number with given number of digits and sum of digits
- Find the Largest number with given number of digits and sum of digits
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Find the average of k digits from the beginning and l digits from the end of the given number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.