Problem Description
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
A single number,
greater than than b.
If not possible,
print -1.
Example 1
Input
459 500
Output
549
Example 2
Input
645757 457765
Output
465577
Example 3
Input
5964 9984
Output
-1
This comment has been removed by the author.
ReplyDeleteans:
ReplyDeletedef permutations(l):
if len(l)==0:
return []
if len(l)==1:
return [l]
current_permut = []
for i in range(len(l)):
m = l[i]
remain_l = l[:i]+l[i+1:]
for p in permutations(remain_l):
current_permut.append([m] + p)
return current_permut
if __name__ == "__main__":
a, b = map(int,input().split())
check = list(str(b))
ans = sorted(permutations(list(str(a))))
c = list()
for i in ans:
if i>check:
c = i
break
if c==[]:
print(-1)
for j in c:
print(j,end = "")
a=input()
ReplyDeletels=list(a)
b=input()
if len(b)!=len(a):
print("-1")
b=int(b)
ls2=[]
from itertools import permutations
# Get all permutations of length 2
# and length 2
perm = permutations(ls, 3)
# Print the obtained permutations
ls1=list(perm)
def convert(list3):
# Converting integer list to string list
s = [str(i) for i in list3]
# Join list items using join()
res = int("".join(s))
return(res)
# Driver code
for i in ls1:
m=list(i)
n=convert(m)
ls2.append(n)
print(ls2)
ls3=[]
for i in ls2:
if i>=b:
ls3.append(i)
if len(ls3)==0:
print("-1")
else:
print(min(ls3))
#include
ReplyDeleteusing namespace std;
int min2=INT_MAX;
void permutate(string sa,int b,int l,int r){
if(l==r){
stringstream geek(sa);
int a = 0;
geek >> a;
if(a>b && a>a>>b;
stringstream ss,ss2;
ss << a;
string sa=ss.str();
permutate(sa,b,0,sa.size()-1);
if(min2==INT_MAX){
cout<<"-1";
}
else
cout<<min2;
}
#include
ReplyDeleteusing namespace std;
int main(){
string s;
string s2;
cin>>s;
cin>>s2;
if(s2<s){
next_permutation(s2.begin(),s2.end());
cout<<s2;
}
else{
while(s<=s2){
next_permutation(s.begin(),s.end());
}
cout<<s;
}
}
#include
ReplyDeleteusing namespace std;
int main()
{
long int a,b,c=0;
cin>>a>>b;
string s = to_string(a);
sort(s.begin(),s.end());
do{
if(stoi(s)>b)
{
cout<<s;
c = 1;
break;
}
}
while(next_permutation(s.begin(),s.end()));
if(c == 0)
cout <<"-1";
return 0;
}