This Exchange digits Problem had been asked in the TCS CodeVita examination in 2019. Today i will give you the solution of Exchange digits TCS CodeVita Problem.

Get All TCS CodeVita Questions with Solution

## Problem Description Of Exchange Digit:-

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.

Input Format

2 numbers a and b, separated by space.

Output Format

A single number greater than b.

If not possible, print -1

Constraints

1 <= a,b <= 10000000

#### Example 1:

Sample Input:

459 500

Sample Output:

549

Example 2:

Sample Input:

645757 457765

Sample Output:

465577

NOTE:- Before viewing the solution try to solve it on your own

## Solution of Exchange Digits:-

# www.TechWithCode.com

from itertools import permutations

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

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

flag = 0

num1 = list(str(num1))

num1 = sorted(num1)

perm = permutations(num1)

for i in list(perm):

#initialize an string

string = " "

#iterate through an string

for j in i:

string+=j

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)

Output of Exchange digits problem:-

Final Word:-

In this article, We have explained to you the Exchange digits TCS CodeVita Problem with a solution. We hope you like it

Don't forget to share how you like this Content in the comment below. If you have any doubt or suggestion then please write in the comment section below, we will love to hear from you.