A charming number is a number which only consist 3 and 8 as its digits. We are given a n-digit number you have total of three operations:
- Adding 1 to given number.
- Subtracting 1 to given number.
- Selecting a digit of number and replacing it with any other desired digit.
We need to find the total number of operations to change given number to charming number.
Input : num = 343 Output : Minimum Operation = 1 Input : num = 88 Output : Minimum operation = 0
Before moving to proper solution let’s have closer look on given operations.
- Adding 1 to given number, it will count 1 operation and what it can do is increment last digit by 1 unless it is 9 and if last digit is 9 then it will also change the digits preceding last one. So in the best case if last digit is 2 or 7 this operation will change them to 3 or 8 on the cost of 1 operation.
- Subtracting 1 to given number, will also count 1 as operation and only decrease the last digit unless it is 0. So in the best case if last digit is 4 or 9 this operation will change them to 3 or 8 on the cost of 1 operation.
- Selecting any digit and changing its value will count as a single operation but for sure it will change digit to charming digit i.e. 3 or 8. So, in the best as well as worst case this operation will change a single digit at once.
So, for finding the minimum operation addition and subtraction is not going to be useful for us and only the number of digits which are not equal to 3 or 8 are going to be selected and changed. So, total number of digit not equal to 3 or 8 will be our answer.
Minimum Operations = 4
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Find smallest number with given number of digits and sum of digits under given constraints
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Find smallest number with given number of digits and sum of digits
- Find the Largest number with given number of digits and sum of digits
- Find the average of k digits from the beginning and l digits from the end of the given number
- Minimum number of digits to be removed so that no two consecutive digits are same
- Smallest number with given sum of digits and sum of square of digits
- Check if the sum of digits of number is divisible by all of its digits
- Sum of the digits of square of the given number which has only 1's as its digits
- Number of digits in the nth number made of given four digits
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Program to Convert Octal Number to Binary Number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.