You are given a positive integer num
. You may swap any two digits of num
that have the same parity (i.e. both odd digits or both even digits).
Return the largest possible value of num
after any number of swaps.
Example 1:
Input: num = 1234 Output: 3412 Explanation: Swap the digit 3 with the digit 1, this results in the number 3214. Swap the digit 2 with the digit 4, this results in the number 3412. Note that there may be other sequences of swaps but it can be shown that 3412 is the largest possible number. Also note that we may not swap the digit 4 with the digit 1 since they are of different parities.
Example 2:
Input: num = 65875 Output: 87655 Explanation: Swap the digit 8 with the digit 6, this results in the number 85675. Swap the first digit 5 with the digit 7, this results in the number 87655. Note that there may be other sequences of swaps but it can be shown that 87655 is the largest possible number.
Constraints:
1 <= num <= 109
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding the largest number after digit swaps by parity means trying every possible combination of swaps. We generate each possible new number by swapping digits of the same parity and see which one is the largest.
Here's how the algorithm would work step-by-step:
def largest_number_after_digit_swaps_by_parity_brute_force(number):
number_string = str(number)
digits_length = len(number_string)
possible_numbers = [number_string]
# Generate all possible swapped numbers.
for first_digit_index in range(digits_length):
for second_digit_index in range(first_digit_index + 1, digits_length):
first_digit = int(number_string[first_digit_index])
second_digit = int(number_string[second_digit_index])
# Only swap if both digits are even or both are odd.
if (first_digit % 2) == (second_digit % 2):
new_number_string = list(number_string)
new_number_string[first_digit_index], new_number_string[second_digit_index] = \
new_number_string[second_digit_index], new_number_string[first_digit_index]
possible_numbers.append("".join(new_number_string))
# Convert all string representations to integers.
possible_numbers_int = [int(number) for number in possible_numbers]
# Find the largest number among all possibilities.
largest_number = number
# Ensure to include the original number too
for possible_number in possible_numbers_int:
if possible_number > largest_number:
largest_number = possible_number
return largest_number
To get the largest possible number, we want to put the biggest digits in the most significant places. We'll separate the even and odd digits, then strategically place them back in the original number.
Here's how the algorithm would work step-by-step:
def largest_number_after_digit_swaps_by_parity(number):
number_string = str(number)
even_digits = []
odd_digits = []
for digit in number_string:
digit_value = int(digit)
if digit_value % 2 == 0:
even_digits.append(digit_value)
else:
odd_digits.append(digit_value)
even_digits.sort(reverse=True)
odd_digits.sort(reverse=True)
even_index = 0
odd_index = 0
result = ''
# Building the result string by picking the largest digits
for digit in number_string:
digit_value = int(digit)
if digit_value % 2 == 0:
# Preserve parity by using digits of the same parity only.
result += str(even_digits[even_index])
even_index += 1
else:
# Preserve parity by using digits of the same parity only.
result += str(odd_digits[odd_index])
odd_index += 1
return int(result)
Case | How to Handle |
---|---|
Input num is 0 | The algorithm should correctly handle 0 and return '0'. |
Input num is a single-digit number | The algorithm should return the input number as a string without any changes. |
Input num has all even digits | The algorithm should sort the even digits in descending order and return the result. |
Input num has all odd digits | The algorithm should sort the odd digits in descending order and return the result. |
Input num has digits in descending order already | The algorithm should return the input number as a string since no swaps are needed. |
Large input number (e.g., a number with 100 digits) | The algorithm's time complexity should be considered; sorting digits is generally efficient, but ensure no excessive memory is used by converting to string for processing. |
Leading zeros after swaps | The algorithm should handle leading zeros correctly after potentially swapping digits to achieve the largest number (leading zeros are acceptable in the result string). |
Integer overflow when converting back to integer | The solution should work with the number as a string to avoid integer overflow during intermediate calculations or final representation. |