A diagnostic switch panel has n toggle switches arranged in a row, each either ON (reported as the character '1') or OFF (reported as '0'). Reading the panel left to right as a binary number gives its diagnostic code. Panel safety rules require the rightmost switch to always be ON, so that the diagnostic code is always odd -- an even code is treated as a fault signature and is forbidden.
You are given the current states of the switches as a string s (in no particular order). You may rearrange the switches into any order you like, using every switch exactly once, so the rearranged panel still has the same number of ON switches and the same number of OFF switches it started with. Among all rearrangements whose rightmost switch is ON, output the one whose binary reading is the largest possible. It is guaranteed that s contains at least one '1', so a valid rearrangement always exists.
A single line containing the string s, consisting only of the characters '0' and '1'.
A single line containing the rearranged string that achieves the maximum odd binary reading.
Example 1
Input
010
Expected
001
Explanation
The panel has one ON switch and two OFF switches. Since the ON switch must occupy the last position, the only possible arrangement places both OFF switches before it, giving 001.
Example 2
Input
0101
Expected
1001
Explanation
With two ON and two OFF switches, one ON switch is reserved for the mandatory last position. The other ON switch should be pushed as far left as possible to maximize the reading, followed by both OFF switches, giving 1001.
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 →