Convert to number with digits as 3 and 8 only
Last Updated :
15 Dec, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int minOp( long long int num)
{
int rem;
int count = 0;
while (num) {
rem = num % 10;
if (!(rem == 3 || rem == 8))
count++;
num /= 10;
}
return count;
}
int main()
{
long long int num = 234198;
cout << "Minimum Operations =" << minOp(num);
return 0;
}
|
Java
public class GFG
{
static int minOp( int num)
{
int rem;
int count = 0 ;
while (num> 0 ) {
rem = num % 10 ;
if (!(rem == 3 || rem == 8 ))
count++;
num /= 10 ;
}
return count;
}
public static void main (String[] args)
{
int num = 234198 ;
System.out.print( "Minimum Operations =" +minOp(num));
}
}
|
Python3
def minOp(num):
count = 0
while (num):
rem = num % 10
if ( not (rem = = 3 or rem = = 8 )):
count = count + 1
num = num / / 10
return count
num = 234198
print ( "Minimum Operations =" ,minOp(num))
|
C#
using System;
class GFG
{
static int minOp( int num)
{
int rem;
int count = 0;
while (num > 0)
{
rem = num % 10;
if (! (rem == 3 || rem == 8))
count++;
num /= 10;
}
return count;
}
public static void Main ()
{
int num = 234198;
Console.WriteLine( "Minimum Operations =" +
minOp(num));
}
}
|
PHP
<?php
function minOp( $num )
{
$count = 0;
while ( $num )
{
$rem = intval ( $num % 10);
if (!( $rem == 3 || $rem == 8))
$count ++;
$num = intval ( $num / 10);
}
return $count ;
}
$num = 234198;
echo "Minimum Operations = " . minOp( $num );
?>
|
Javascript
<script>
function minOp(num)
{
var rem;
var count = 0;
while (num) {
rem = num % 10;
if (!(rem == 3 || rem == 8))
count++;
num = parseInt(num/10);
}
return count;
}
var num = 234198;
document.write( "Minimum Operations = " + minOp(num));
</script>
|
Output
Minimum Operations =4
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...