Reduce N to 1 with minimum number of given operations
Last Updated :
16 Dec, 2022
Given an integer N, the task is to reduce N to 1 with the following two operations:
- 1 can be subtracted from each of the digits of the number only if the digit is greater than 0 and the resultant number doesn’t have any leading 0s.
- 1 can be subtracted from the number itself.
The task is to find the minimum number of such operations required to reduce N to 1.
Examples:
Input: N = 35
Output: 14
35 -> 24 -> 14 -> 13 -> 12 -> 11 -> 10 -> … -> 1 (14 operations)
Input: N = 240
Output: 23
Approach: It can be observed that if the number is power of 10 i.e. N = 10p then the number of operations will be (10 * p) – 1. For example, if N = 102 then operations will be (10 * 2) – 2 = 19
i.e. 100 -> 99 -> 88 -> 77 -> … -> 33 -> 22 -> 11 -> 10 -> 9 -> 8 -> … -> 2 -> 1.
Now, the task is to first convert the given to a power of 10 with the given operations and then count the number of operations required to reduce that power of 10 to 1. The sum of these operations is the required answer. The number of operations required to convert a number to a power of will be max(first_digit – 1, second_digit, third_digit, …, last_digit), this is because every digit can be reduced to 0 but the first digit must be 1 in order for it to be power of 10 with equal number of digits.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long long int minOperations( long long int n)
{
long long int count = 0;
long long int d = 0;
if (n == 1)
return 0;
while (n > 9) {
d = max(n % 10, d);
n /= 10;
count += 10;
}
d = max(d, n - 1);
count += abs (d);
return count - 1;
}
int main()
{
long long int n = 240;
cout << minOperations(n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static long minOperations( long n)
{
long count = 0 ;
long d = 0 ;
if (n == 1 )
return 0 ;
while (n > 9 )
{
d = Math.max(n % 10 , d);
n /= 10 ;
count += 10 ;
}
d = Math.max(d, n - 1 );
count += Math.abs(d);
return count - 1 ;
}
public static void main (String[] args)
{
long n = 240 ;
System.out.println(minOperations(n));
}
}
|
Python3
def minOperations(n):
count = 0
d = 0
if (n = = 1 ):
return 0
while (n > 9 ):
d = max (n % 10 , d)
n / / = 10
count + = 10
d = max (d, n - 1 )
count + = abs (d)
return count - 1
if __name__ = = '__main__' :
n = 240
print (minOperations(n))
|
C#
using System;
class GFG
{
static long minOperations( long n)
{
long count = 0;
long d = 0;
if (n == 1)
return 0;
while (n > 9)
{
d = Math.Max(n % 10, d);
n /= 10;
count += 10;
}
d = Math.Max(d, n - 1);
count += Math.Abs(d);
return count - 1;
}
public static void Main (String[] args)
{
long n = 240;
Console.WriteLine(minOperations(n));
}
}
|
Javascript
<script>
function minOperations( n)
{
var count = 0;
var d = 0;
if (n == 1)
return 0;
while (n > 9) {
d = Math.max(n % 10, d);
n /= 10;
count += 10;
}
d = Math.max(d, n - 1);
count += Math.abs(d);
return count - 1;
}
var n = 240;
document.write(minOperations(n));
</script>
|
Time Complexity: O(log10n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...