A warehouse's dispatch barcode is printed as a long string of digits with no leading zero. The scanner at the loading dock sometimes misreads the last few printed digits, so the dispatch protocol requires trimming zero or more digits from the right end of the barcode until the last remaining digit is odd -- this is the checksum convention that marks a code as valid for shipping. Among all barcodes obtainable this way, the dock supervisor wants the greatest possible numeric value. Given the original barcode, report that greatest value, or report that no valid trim exists if every digit in the barcode is even (in which case trimming all the way down never lands on an odd digit).
A single line containing the barcode s, a string of decimal digits whose first character is never '0'.
Print the greatest numeral obtainable by deleting zero or more digits from the right end of s so that the last remaining digit is odd, with no leading or trailing whitespace. If no such trim exists (every digit of s is even), print -1 instead.
Example 1
Input
1234567
Expected
1234567
Explanation
The barcode already ends in 7, which is odd, so no trimming is required and the greatest value obtainable is the barcode itself, 1234567.
Example 2
Input
246820
Expected
-1
Explanation
Every digit of 246820 (2, 4, 6, 8, 2, 0) is even, so no amount of trimming from the right ever produces an odd last digit. The answer is -1.
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 →