Math, asked by pbhaturkar5, 11 months ago

Compute nearest larger number by interchanging 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.

Constraints

1 <= a,b <= 10000000

Input Format

2 numbers, a and b, separated by space.

Output Format

A single number, greater than b.

If not possible, print -1.

Example 1

Input

459 500

Output

549

Example 2

Input

645757 457765

Output

465577

Answers

Answered by saurabhaadi4
0

Answer:

1 <= a,b <= 10000000

Input Format

2 numbers, a and b, separated by space.

Output Format

A single number, greater than b.

If not possible, print -1.

Example 1

Input

459 500

Output

549

Example 2

Input

645757 457765

Output

465577

Answered by vits19731a05j2
0

Answer:

import itertools to get permutation function

from itertools import permutations

#take inputs

num1 = int(input('Enter the 1st number :'))

num2 = int(input('Enter the 2nd number :'))

#initialize a flag variable

flag = 0

#convert num1 to string list

num1 = list(str(num1))

#sort the list

num1 = sorted(num1)

#find all permutations

perm = permutations(num1)

#iterate through all permutations

for i in list(perm):

#initialize an string

string = " "

#iterate through an string

for j in i:

string+=j

#typecast string to integer

#check for next greater value

if int(string) > num2:

#if True Change the flag variable

#break the loop

flag = 1

break

#check if the number is found or not

if flag == 1:

print(string)

else:

print(-1)

Similar questions