Maximum Odd Binary Number - Problem
Imagine you have a collection of binary digits (0s and 1s) that form a binary string, and you want to create the largest possible odd binary number by rearranging these digits.
Given a binary string s that contains at least one '1', your task is to rearrange the bits to form the maximum odd binary number possible.
Key Points:
- A binary number is odd if and only if its last digit is '1'
- To maximize the value, we want as many '1's as possible in the most significant positions
- The result can have leading zeros (they don't affect the value)
Example: If you have "0011", you can rearrange it to "1101" which is the maximum odd number possible.
Input & Output
example_1.py โ Basic Case
$
Input:
s = "010"
โบ
Output:
"101"
๐ก Note:
We have one '1' and two '0's. To make the maximum odd number, we place the '1' at the front and keep one '1' at the end. Since we only have one '1' total, it must go at the end, giving us "101".
example_2.py โ Multiple 1s
$
Input:
s = "0101"
โบ
Output:
"1001"
๐ก Note:
We have two '1's and two '0's. We place (2-1)=1 '1' at the front, then all '0's in the middle, and finally one '1' at the end: '1' + '00' + '1' = "1001".
example_3.py โ All 1s
$
Input:
s = "111"
โบ
Output:
"111"
๐ก Note:
When all characters are '1', we place (3-1)=2 '1's at the front and one '1' at the end. Since there are no '0's, the result is "111".
Constraints
- 1 โค s.length โค 100
- s consists only of '0' and '1'
- s contains at least one '1'
Visualization
Tap to expand
Understanding the Visualization
1
Count Your Resources
Count how many 1s (valuable) and 0s (fillers) you have
2
Maximize Front Value
Place as many 1s as possible at the front for maximum value
3
Save One 1 for End
Reserve exactly one 1 for the last position to ensure oddness
4
Fill Middle with 0s
Place all 0s between the front 1s and the last 1
Key Takeaway
๐ฏ Key Insight: To maximize an odd binary number, place all 1's except one at the front for maximum value, and exactly one 1 at the end for oddness.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code