Back to articles
PWC 367 Overlapping Oddities

PWC 367 Overlapping Oddities

via Dev.toBob Lied

Task 1, Maximum Odd Binary It's the week of the Artemis 2 mission to the moon, and we have a problem about odd numbers. I'd call that a Space Oddity . Task Description You are given a binary string that has at least one ‘1’. Write a script to rearrange the bits in such a way that the resulting binary number is the maximum odd binary number and return the resulting binary string. The resulting string can have leading zeros. Example 1: Input: $str = "1011" , Output: "1101" Example 2: Input: $str = "100" , Output: "001" Example 3: Input: $str = "111000" , Output: "110001" Example 4: Input: $str = "0101" , Output: "1001" Example 5: Input: $str = "1111" , Output: "1111" Task Cogitation To be as large as possible, a binary number must have all its 1s in the most-significant bits. To be odd, the least significant bit must be a one. So, what has to happen is that all the 1s shift to the left, one shifts to the right, and all the zeroes form a space between them. I see three possible solutions:

Continue reading on Dev.to

Opens in a new tab

Read Full Article
2 views

Related Articles