Meta VO Interview Question: Rearrange Digits (Smallest Number from Odd Digits)

16 Views
No Comments

Write a function that returns the smallest nonnegative number which can be generated by using all the digits with odd values of a given number.

Example:

number = 10430
Result: 13

The key idea is to extract only the odd digits from the input number, sort them in ascending order, and join them to form the smallest possible nonnegative result. For example, 10430 contains the odd digits 1 and 3, so the answer is 13. If there are no odd digits, the function should return None. A straightforward solution is to convert the number to a string, filter odd digits, sort them, and rebuild the number. The work is proportional to the number of digits.

END
 0