using
System;
using
System.Collections.Generic;
class
GFG {
public
static
string
swap(String str,
int
i,
int
j)
{
char
[] ch = str.ToCharArray();
char
temp = ch[i];
ch[i] = ch[j];
ch[j] = temp;
return
new
string
(ch);
}
public
static
string
sortString(
string
str,
int
s_index,
int
e_index)
{
char
[] tempArray = str.ToCharArray();
Array.Sort(tempArray, s_index, e_index - s_index);
return
new
string
(tempArray);
}
public
static
string
maximizeNumber(
string
N,
int
M)
{
N = sortString(N, 0, N.Length);
for
(
int
i = 0; i < N.Length; i++) {
for
(
int
j = i + 1; j < N.Length; j++) {
string
t = N;
t = swap(t, j, i);
t = sortString(t, i + 1, t.Length);
if
(Convert.ToInt64(t) > Convert.ToInt64(N)
&& Convert.ToInt64(t) <= M)
N = swap(N, i, j);
}
}
return
N;
}
public
static
void
Main(
string
[] args)
{
string
N =
"123"
;
int
M = 222;
Console.WriteLine(maximizeNumber(N, M));
}
}