Maximum Odd Binary Number - Problem

You are given a binary string s that contains at least one '1'.

You have to rearrange the bits in such a way that the resulting binary number is the maximum odd binary number that can be created from this combination.

Return a string representing the maximum odd binary number that can be created from the given combination.

Note that the resulting string can have leading zeros.

Input & Output

Example 1 — Basic Case
$ Input: s = "010"
Output: "001"
💡 Note: We have one '1' and two '0s. To maximize and keep odd: place 0 '1's at front (since 1-1=0), two '0's in middle, and one '1' at end → "001"
Example 2 — Multiple 1s
$ Input: s = "101"
Output: "101"
💡 Note: We have two '1's and one '0'. Place 1 '1' at front, one '0' in middle, one '1' at end → "101". This is already optimal.
Example 3 — Many 1s
$ Input: s = "1111"
Output: "1111"
💡 Note: All bits are '1'. Place 3 '1's at front, no '0's in middle, one '1' at end → "1111". Already maximum odd number.

Constraints

  • 1 ≤ s.length ≤ 100
  • s consists only of '0' and '1'
  • s contains at least one '1'

Visualization

Tap to expand
Maximum Odd Binary Number: "10101"Input String10101Rearrange StrategyCount: 3 ones, 2 zerosPut 2 ones at front + 2 zeros + 1 one at endMaximum Odd Result11001
Understanding the Visualization
1
Input
Binary string with mixed 1s and 0s
2
Process
Rearrange to maximize while keeping odd
3
Output
Maximum possible odd binary number
Key Takeaway
🎯 Key Insight: Place all 1s at front for maximum value, except keep one 1 at end to ensure odd
Asked in
Google 25 Amazon 20 Microsoft 15
32.0K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen