Given an integer N, the task is to find the maximum difference after applying the given operation two times on the given integer. The operation is defined as follows:
- Choose any digit (0-9) from N and replace all instance of the same digit with any other digit (0-9).
- N after the operation cannot have leading zeros and also it cannot be equal to zero.
Input : N = 777
Output : 888
Explanation: Select digit 7 and replace all it’s occurrences with 9 to get 999. similarly 111 can be obtained by replacing all occurrences of 7 with 1. Hence, the possible maximum difference is 888.
Input : N = 123456
Output : 820000
The numbers after two operation can be 923456 and 103456. Hence, the required maximum difference is 820000.
Explanation: Maximum difference can be obtained by subtraction of the maximum and the minimum number that can be obtained by the given operation on N.
- The maximum number can be obtained by picking the first digit of N from the left which is not equal to ‘9’ and replace all the instances of that digit into ‘9’.
- Finding the minimum number is a little bit tricky as N cannot have any leading zeros and also it cannot be equal to zero. If the first digit of N from the left is ‘1’, then find the first digit from the left which is greater than ‘1’ and replace all instances of that digit with ‘0’.
- Otherwise, if the first digit of N from the left is not equal to ‘1’, then choose that digit and replace all instances of that digit with ‘1’.
- Finally, return the difference between the minimum and maximum number as the answer.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimizing array sum by applying XOR operation on all elements of the array
- Find maximum value of the last element after reducing the array with given operations
- Maximum score after flipping a Binary Matrix atmost K times
- Maximum sum of all elements of array after performing given operations
- Maximum possible Array sum after performing given operations
- Maximum possible sum after M operations on N cards
- Minimum element left from the array after performing given operations
- Cost required to empty a given array by repeated removal of maximum obtained by given operations
- Generate a number such that the frequency of each digit is digit times the frequency in given number
- Minimize cost to convert given two integers to zero using given operations
- Number of times an array can be partitioned repetitively into two subarrays with equal sum
- Lexicographically smallest string after M operations
- Find if k bookings possible with given arrival and departure times
- Find Nth smallest number that is divisible by 100 exactly K times
- Maximum profit by buying and selling a share at most K times | Greedy Approach
- Minimum number of steps required to obtain the given Array by the given operations
- Reduce a given number to form a key by the given operations
- Number of times the largest perfect square number can be subtracted from N
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
- Minimize the maximum minimum difference after one removal from array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.