Given two numbers A and B, the task is to find the arrangement of digits of A such that it is just greater than the given number B, i.e., to find the minimum value permutation of A greater than B. If no such permutation is possible then print -1
Input: A = 9236, B = 3125
The minimum number greater than 3125 formed from the digits of A is 3269.
Input: A = 1234, B = 9879
- Take both the numbers as String input to make use of next_permutation().
- Use stol() to find the long value of B.
- Then find the lowest permutation of the number A.
- For each permutation of A, check whether the number is greater than B.
- If any permutation is greater than the number B then it is one of the possible answers. Select the minimum of all the possible answers.
- If no such number is present print -1.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Smallest subarray of size greater than K with sum greater than a given value
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Minimum number of given operations required to convert a permutation into an identity permutation
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Largest number less than N with digit sum greater than the digit sum of N
- Highest and Smallest power of K less than and greater than equal to N respectively
- Minimum element whose n-th power is greater than product of an array of size n
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Minimum value to be assigned to the elements so that sum becomes greater than initial sum
- Minimum value of K such that sum of cubes of first K natural number is greater than equal to N
- Find the Next perfect square greater than a given number
- Find the number of elements greater than k in a sorted array
- Find K such that changing all elements of the Array greater than K to K will make array sum N
- Find the next Factorial greater than N
- Find a number K such that exactly K array elements are greater than or equal to K
- Maximum difference between frequency of two elements such that element having greater frequency is also greater
- Largest subsequence having GCD greater than 1
- Count numbers with difference between number and its digit sum greater than specific value
- Number of subarrays having absolute sum greater than K | Set-2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.