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

**Examples:**

Input:A = 9236, B = 3125Output:3269Explanation:

The minimum number greater than 3125 formed from the digits of A is 3269.

Input:A = 1234, B = 9879Output:-1

**Approach:** The idea is to use next_permutation() and stol(). The following steps can be followed to compute the answer:

- 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:

`// C++ program to find the greater permutation ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; `
`#define ll long long ` `#define inf 999999999999999999 ` ` ` `// Function to find the greater permutation ` `ll solve(string a, string b) ` `{ ` ` ` `ll n, val, ans = inf; `
` ` ` ` `// Convert the string B to long `
` ` `val = stol(b); `
` ` `n = a.length(); `
` ` ` ` `// To find the lowest permutation `
` ` `// of the number `
` ` `sort(a.begin(), a.end()); `
` ` ` ` `// Find if the lowest permutation of A is `
` ` `// greater than the given number B `
` ` `if` `(stol(a) > val) { `
` ` `ans = min((ll)stol(a), ans); `
` ` `} `
` ` ` ` `// Find all the permutations of A `
` ` `while` `(next_permutation(a.begin(), `
` ` `a.end())) { `
` ` `if` `(stol(a) > val) { `
` ` `ans = min((ll)stol(a), ans); `
` ` `} `
` ` `} `
` ` ` ` `// If ans is not the initial value `
` ` `// then return ans `
` ` `if` `(ans != inf) { `
` ` `return` `ans; `
` ` `} `
` ` `// Else return -1 `
` ` `else` `{ `
` ` `return` `-1; `
` ` `} `
`} ` ` ` `// Driver code ` `int` `main() `
`{ ` ` ` `string a, b; `
` ` `ll ans; `
` ` `a = ` `"9236"` `; `
` ` `b = ` `"3145"` `; `
` ` `ans = solve(a, b); `
` ` `cout << ans; `
`} ` |

*chevron_right*

*filter_none*

**Output:**

3269

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.

## Recommended Posts:

- 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 contribute@geeksforgeeks.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.