Array ( [0] => [1] => questions [2] => Codevita-Previous-Questions [3] => Exchange-Of-Digits )
Compute the nearest larger number by interchanging its digits updated.Given 2 numbers a and b find the smallest number greater than b by interchanging the digits of a and if not possible print -1.
2 numbers a and b, separated by space.
Constraints:
1 <= a,b <= 10000000
A single number greater than b.
If not possible, print -1
459 500
549
645757 457765
465577
459 500
549
Login to see Discussion
Find all the permutations for the first number, sort the permutations and initialize the flag value as 0. Iterate through each permutation and compare it with second number, if the permutation is greater than the second number change the flag value to 1. At last, is the flag value is 1 print the number and if flag is 0 print '-1'.
Note :
Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.
Login to see Solution