

You are given a string ‘S’. Your task is to find the largest even number that can be formed by rearranging string S’s digits.
Note:
In case the string does not contain any even digit, then return -1.
For example,
Given string SS = “21”
The output will be 12 since it is the largest even number is formed by swapping 2 and 1.
Input format :
The first line of input contains an integer T denoting the number of test cases.
The first and the only line of each test case contains a single string ‘S’.
Output format :
For each test case, return the max possible even number that can be formed. The output of each test case will be printed in a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 5
1 <= |S| <= 3000
0 <= S[i] <= 9
Where |S| denotes the length of the given string 'S'.
Time Limit: 1 sec
2
1002
2242
2100
4222
For the first test case:
Given string S = “1002”, therefore, first, swap 1(index 0) and 2(index 3) and then swap 0(index 1) and 1(index 3), which gives 2100 which is the largest number that can be formed from this string.
For the second test case:
Swap 4(index 2) with the 2(index 0) which gives 4222 which is the largest number that can be formed from this string.
2
1234
135
4312
-1
Will sorting help?
The main idea is to sort the string in descending order, then put the minimum even number at the last position. If no even number is present, then put the minimum number at the last position.
O( N*log(N) ), where N is the size of the string.
Since we are sorting the string and then traversing the string. O(N*log(N) + N) = O(N*log(N)).
O(1).
Since we are not using any extra space.