Similar Problems

Similar Problems not available

Maximum Difference By Remapping A Digit - Leetcode Solution

Companies:

LeetCode:  Maximum Difference By Remapping A Digit Leetcode Solution

Difficulty: Easy

Topics: greedy math  

Problem Statement: Given an integer n, you need to change the digits of n and arrange them in such a way that the resulting number is maximum. The resulting number should not contain any leading zeros. Also, the difference between the maximum and minimum numbers that you can get should be as large as possible.

Example:

Input: n = 284 Output: 820 Explanation: You can rearrange the digits as 482 or 824, but 824 - 482 = 342, which is not the maximum possible difference.

Solution: To solve this problem, we need to find the maximum and minimum possible numbers using the given digits.

Firstly, we need to find the maximum possible number that we can make by rearranging the given digits. To do so, we need to sort the digits in descending order and then concatenate them to form the number.

Secondly, we need to find the minimum possible number that we can make by rearranging the given digits. To do so, we need to sort the digits in ascending order and then concatenate them to form the number.

Finally, we need to calculate the difference between the maximum and minimum numbers that we can get. This difference will be the maximum possible difference that we can get by remapping a digit.

Pseudo code:

  1. Sort the digits of the given number n in descending and ascending order to form the maximum and minimum numbers respectively.

    max_num = sort(n, descending_order) min_num = sort(n, ascending_order)

  2. Remove leading zeros, if any, from the maximum and minimum numbers.

    max_num = remove_leading_zeros(max_num) min_num = remove_leading_zeros(min_num)

  3. Calculate the difference between the maximum and minimum numbers.

    diff = max_num - min_num

  4. Return the difference as the maximum possible difference.

Steps 1 to 3 can be implemented in a single function called max_diff_by_remapping.

Code:

def max_diff_by_remapping(n): """Returns the maximum possible difference that can be obtained from the given number n by remapping a digit."""

# Step 1: Sort the digits in descending and ascending order to form maximum
#         and minimum numbers respectively.
max_num = int(''.join(sorted(str(n), reverse=True)))
min_num = int(''.join(sorted(str(n))))

# Step 2: Remove leading zeros, if any, from the maximum and minimum numbers.
max_num = int(str(max_num).lstrip('0')) if str(max_num).lstrip('0') else 0
min_num = int(str(min_num).lstrip('0')) if str(min_num).lstrip('0') else 0

# Step 3: Calculate the difference between the maximum and minimum numbers.
diff = max_num - min_num

# Step 4: Return the difference as the maximum possible difference.
return diff

Testing:

assert(max_diff_by_remapping(284) == 536) assert(max_diff_by_remapping(123456) == 864210) assert(max_diff_by_remapping(100) == 99) assert(max_diff_by_remapping(4321) == 3087)

Time Complexity: O(d*log(d)), where d is the number of digits in the given number n.

Space Complexity: O(d), where d is the number of digits in the given number n.

Maximum Difference By Remapping A Digit Solution Code

1