#K73657. Maximum Even Number
Maximum Even Number
Maximum Even Number
Given a string s consisting of digits, rearrange all its digits to form the maximum possible even number. You must use every digit exactly once. If it is impossible to form an even number, output -1
.
The solution involves sorting the digits in descending order, then locating an even digit to place at the end. Formally, if the digits are represented as \(d_1, d_2, \ldots, d_n\), you need to form the number \(N = d_1d_2\ldots d_n\) such that \(N\) is even and as large as possible. If no even digit exists among the digits, output \(-1\).
Example:
- Input:
4321
→ Output:4312
- Input:
13579
→ Output:-1
inputFormat
The input consists of a single line containing a non-empty string s of digits.
outputFormat
Output the maximum possible even number formed by rearranging the digits of s exactly once. If it is not possible to form an even number, output -1
.
4321
4312