Leetcode 2864: Maximum Odd Binary Number

grid47
grid47
Exploring patterns and algorithms
Jan 25, 2024 4 min read

You are given a binary string s containing at least one ‘1’. Your task is to rearrange the bits in such a way that the resulting binary number is the largest possible odd binary number that can be formed from the given bits. A binary number is odd if its least significant bit (last bit) is ‘1’. The resulting binary number may contain leading zeros.
Problem
Approach
Steps
Complexity
Input: The input consists of a binary string s containing only '0' and '1' characters. The string will contain at least one '1'.
Example: s = "100"
Constraints:
• 1 <= s.length <= 100
• s contains only '0' and '1'.
• s contains at least one '1'.
Output: Return a string representing the largest odd binary number that can be created from the given binary string.
Example: For input s = "100", the output is "010".
Constraints:
Goal: The goal is to rearrange the bits in such a way that the resulting binary number is the largest odd binary number possible.
Steps:
• Identify and place the '1' in the last position to ensure the binary number is odd.
• Sort the remaining bits in descending order to form the largest possible binary number.
Goal: The problem requires a rearrangement of the bits while keeping the last bit '1' to ensure the binary number is odd.
Steps:
• 1 <= s.length <= 100
• The string contains at least one '1'.
Assumptions:
• The binary string contains at least one '1', ensuring that a valid odd binary number can be created.
Input: For input s = "100", the output is "010".
Explanation: To create the largest odd binary number, the '1' must be placed in the last position. The remaining bits '10' can be rearranged as "01", resulting in the binary number "010".

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus