A stadium's mechanical scoreboard displays a positive whole number as a row of flip tiles, one decimal digit per tile. The maintenance crew can repeatedly pick any two tiles and swap their digits, but only when both digits have the same parity — both odd, or both even. Swapping an odd-digit tile with an even-digit tile is never allowed. Starting from the number currently shown, the crew wants to know the largest number the scoreboard could ever display after performing any number of these swaps (including zero).
A single line containing the number currently shown on the scoreboard, written with no leading zero.
Print the largest number achievable, written with no leading zero.
Example 1
Input
6395
Expected
6953
Explanation
Digit 6 is even and is alone in its group, so it stays put. Digits 3, 9, 5 are all odd; sorted from largest to smallest they are 9, 5, 3. Rebuilding left to right: position 0 keeps the even slot's digit (6), and positions 1, 2, 3 take the sorted odd digits in order (9, then 5, then 3), giving "6953".
Example 2
Input
280
Expected
820
Explanation
Every digit — 2, 8, and 0 — is even, so they all belong to one group and can be freely reordered among themselves. Sorted from largest to smallest they are 8, 2, 0, so the scoreboard can be rearranged to read "820", the maximum possible.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →