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.

Examples:

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.

## C++

`// CPP to find min operations required to ` `// convert into charming number ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function for minimum operation ` `int` `minOp(` `long` `long` `int` `num) ` `{ ` ` ` `// remainder and operations count ` ` ` `int` `rem; ` ` ` `int` `count = 0; ` ` ` ` ` `// count digits not equal to 3 or 8 ` ` ` `while` `(num) { ` ` ` `rem = num % 10; ` ` ` `if` `(!(rem == 3 || rem == 8)) ` ` ` `count++; ` ` ` `num /= 10; ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` `// driver function ` `int` `main() ` `{ ` ` ` `long` `long` `int` `num = 234198; ` ` ` `cout << ` `"Minimum Operations ="` `<< minOp(num); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java to find min operations required to ` `// convert into charming number ` `class` `GFG ` `{ ` ` ` ` ` `// function for minimum operation ` ` ` `static` `int` `minOp(` `int` `num) ` ` ` `{ ` ` ` ` ` `// remainder and operations count ` ` ` `int` `rem; ` ` ` `int` `count = ` `0` `; ` ` ` ` ` `// count digits not equal to 3 or 8 ` ` ` `while` `(num>` `0` `) { ` ` ` `rem = num % ` `10` `; ` ` ` `if` `(!(rem == ` `3` `|| rem == ` `8` `)) ` ` ` `count++; ` ` ` `num /= ` `10` `; ` ` ` `} ` ` ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `num = ` `234198` `; ` ` ` ` ` `System.out.print(` `"Minimum Operations ="` `+minOp(num)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## Python3

`# Python to find min ` `# operations required to ` `# convert into charming number ` ` ` `# function for minimum operation ` `def` `minOp(num): ` ` ` ` ` `# remainder and operations count ` ` ` `count ` `=` `0` ` ` ` ` `#count digits not equal to 3 or 8 ` ` ` `while` `(num): ` ` ` `rem ` `=` `num ` `%` `10` ` ` `if` `(` `not` `(rem ` `=` `=` `3` `or` `rem ` `=` `=` `8` `)): ` ` ` `count` `=` `count ` `+` `1` ` ` `num ` `=` `num ` `/` `/` `10` ` ` ` ` `return` `count ` ` ` `# Driver code ` ` ` `num ` `=` `234198` `print` `(` `"Minimum Operations ="` `,minOp(num)) ` ` ` `# This code is contributed ` `# by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# to find min operations required to ` `// convert into charming number ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// function for minimum operation ` ` ` `static` `int` `minOp(` `int` `num) ` ` ` `{ ` ` ` ` ` `// remainder and operations count ` ` ` `int` `rem; ` ` ` `int` `count = 0; ` ` ` ` ` `// count digits not equal to 3 or 8 ` ` ` `while` `(num > 0) ` ` ` `{ ` ` ` `rem = num % 10; ` ` ` `if` `(! (rem == 3 || rem == 8)) ` ` ` `count++; ` ` ` `num /= 10; ` ` ` `} ` ` ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `num = 234198; ` ` ` ` ` `Console.WriteLine(` `"Minimum Operations ="` `+ ` ` ` `minOp(num)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP to find min operations required to ` `// convert into charming number ` ` ` `// function for minimum operation ` `function` `minOp(` `$num` `) ` `{ ` ` ` ` ` `// remainder and operations count ` ` ` `$count` `= 0; ` ` ` ` ` `// count digits not equal to 3 or 8 ` ` ` `while` `(` `$num` `) ` ` ` `{ ` ` ` `$rem` `= ` `intval` `(` `$num` `% 10); ` ` ` `if` `(!(` `$rem` `== 3 || ` `$rem` `== 8)) ` ` ` `$count` `++; ` ` ` `$num` `= ` `intval` `(` `$num` `/ 10); ` ` ` `} ` ` ` `return` `$count` `; ` `} ` ` ` ` ` `// Driver Code ` ` ` `$num` `= 234198; ` ` ` `echo` `"Minimum Operations = "` `. minOp(` `$num` `); ` ` ` `// This code is contributed by Sam007 ` `?> ` |

*chevron_right*

*filter_none*

Output:

Minimum Operations = 4

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.

## Recommended Posts:

- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Sum of the digits of square of the given number which has only 1's as its digits
- Minimum digits to be removed to make either all digits or alternating digits same
- Minimum number with digits as 4 and 7 only and given sum
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
- Finding n-th number made of prime digits (2, 3, 5 and 7) only
- Smallest multiple of a given number made of digits 0 and 9 only
- Check if the given decimal number has 0 and 1 digits only
- Find the number of integers from 1 to n which contains digits 0's and 1's only
- 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
- 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
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Find the n-th number made of even digits only
- Nth number made up of odd digits only
- Minimum number closest to N made up of odd digits only
- Smallest number greater than or equal to N using only digits 1 to K
- Number of digits in the nth number made of given four digits

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.