Maximum Odd Binary Number (LeetCode 2864): Solution and Explanation

Easy
Math
String
Greedy

Rearrange the bits of a binary string that has at least one 1 to form the largest odd binary number. Leading zeros are allowed.

Intuition

A rearrangement keeps exactly the same bits, so the answer depends only on how many 1s and 0s the string has, not on their positions.

A binary number is odd exactly when its lowest bit, the last character, is 1. So one 1 must be spent on the last position.

Among the remaining positions, a 1 in a higher position is worth more than all lower positions combined. So every other 1 should sit as far left as possible.

That gives ones - 1 ones, then all the zeros, then a final 1.

Approach: Count, then place greedily

  1. Count the 1s in s; the zeros are len(s) - ones.
  2. Build ones - 1 ones for the front.
  3. Append zeros zeros.
  4. Append the reserved 1 at the end and return the string.

Step-by-step example

s = "0101"
  1. Count: two 1s and two 0s.
  2. Reserve one 1 for the last slot.
  3. Front: the remaining one 1 gives "1".
  4. Middle: two zeros give "100".
  5. Last: append the reserved 1 for "1001".

Code: Python, Java, C++

1class Solution:
2    def maximumOddBinaryNumber(self, s: str) -> str:
3        ones = 0
4        for ch in s:
5            if ch == '1':
6                ones += 1
7        zeros = len(s) - ones
8        lead = '1' * (ones - 1)
9        middle = '0' * zeros
10        return lead + middle + '1'
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Count, then place greedily O(n)
In-place two pointers O(n)
Sort then fix the last bit O(n log n)
Time

O(n)

One pass to count and one pass to build the output.

Space

O(n)

The output string has n characters; the counting itself uses O(1).

Other approaches

Sort then fix the last bit (time O(n log n), space O(n))

Sort the characters in descending order so all 1s come first, then move one 1 from the front to the end. Correct, but sorting is unnecessary for two symbols.

In-place two pointers (time O(n), space O(n))

On a character array, swap 1s to the front with a write pointer, then swap the last 1 written with the final position.

Common mistakes

  • Putting all the 1s at the front, which makes the number even whenever there is at least one zero.
  • Writing the reserved 1 at the front instead of the end. Odd means the lowest bit, the last character, is 1.
  • Forgetting the case with a single 1: the front is empty and the answer is all zeros then a 1.
  • Converting to an integer. With up to 100 bits, the value overflows 64-bit types.

Frequently asked questions

Why must one 1 go at the end?

The last character is the 2^0 bit. Every other bit adds an even amount, so the number is odd only if that last bit is 1.

Why put the remaining 1s first?

A 1 in position k is worth more than all positions below it combined, so moving a 1 to a higher position always increases the value.

Are leading zeros a problem?

No. The problem allows them, and they appear whenever there is only one 1 and at least one 0.