Skip to content
Related Articles

Related Articles

Improve Article

Reduce N to 1 with minimum number of given operations

  • Last Updated : 30 Apr, 2021

Given an integer N, the task is to reduce N to 1 with the following two operations: 
 

  1. 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.
  2. 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++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the minimum number of
// given operations required to reduce n to 1
long long int minOperations(long long int n)
{
    // To store the count of operations
    long long int count = 0;
 
    // To store the digit
    long long int d = 0;
 
    // If n is already then no
    // operation is required
    if (n == 1)
        return 0;
 
    // Extract all the digits except
    // the first digit
    while (n > 9) {
 
        // Store the maximum of that digits
        d = max(n % 10, d);
        n /= 10;
 
        // for each digit
        count += 10;
    }
 
    // First digit
    d = max(d, n - 1);
 
    // Add the value to count
    count += abs(d);
 
    return count - 1;
}
 
// Driver code
int main()
{
    long long int n = 240;
 
    cout << minOperations(n);
 
    return 0;
}

Java




// Java implementation of the approach
class GFG
{
     
    // Function to return the minimum number of
    // given operations required to reduce n to 1
    static long minOperations(long n)
    {
        // To store the count of operations
        long count = 0;
     
        // To store the digit
        long d = 0;
     
        // If n is already then no
        // operation is required
        if (n == 1)
            return 0;
     
        // Extract all the digits except
        // the first digit
        while (n > 9)
        {
     
            // Store the maximum of that digits
            d = Math.max(n % 10, d);
            n /= 10;
     
            // for each digit
            count += 10;
        }
     
        // First digit
        d = Math.max(d, n - 1);
     
        // Add the value to count
        count += Math.abs(d);
     
        return count - 1;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        long n = 240;
     
        System.out.println(minOperations(n));
    }
}
 
// This code is contributed by AnkitRai01

Python3




# Python3 implementation of the approach
 
# Function to return the minimum number of
# given operations required to reduce n to 1
def minOperations(n):
 
    # To store the count of operations
    count = 0
 
    # To store the digit
    d = 0
 
    # If n is already then no
    # operation is required
    if (n == 1):
        return 0
 
    # Extract all the digits except
    # the first digit
    while (n > 9):
 
        # Store the maximum of that digits
        d = max(n % 10, d)
        n //= 10
 
        # for each digit
        count += 10
     
    # First digit
    d = max(d, n - 1)
 
    # Add the value to count
    count += abs(d)
 
    return count - 1
 
# Driver code
if __name__ == '__main__':
 
    n = 240
 
    print(minOperations(n))
 
# This code is contributed by ashutosh450

C#




// C# implementation of the approach
using System;
 
class GFG
{
     
    // Function to return the minimum number of
    // given operations required to reduce n to 1
    static long minOperations(long n)
    {
        // To store the count of operations
        long count = 0;
     
        // To store the digit
        long d = 0;
     
        // If n is already then no
        // operation is required
        if (n == 1)
            return 0;
     
        // Extract all the digits except
        // the first digit
        while (n > 9)
        {
     
            // Store the maximum of that digits
            d = Math.Max(n % 10, d);
            n /= 10;
     
            // for each digit
            count += 10;
        }
     
        // First digit
        d = Math.Max(d, n - 1);
     
        // Add the value to count
        count += Math.Abs(d);
     
        return count - 1;
    }
     
    // Driver code
    public static void Main (String[] args)
    {
        long n = 240;
     
        Console.WriteLine(minOperations(n));
    }
}
 
// This code is contributed by Rajput-Ji

Javascript




<script>
 
// Javascript implementation of the approach
 
// Function to return the minimum number of
// given operations required to reduce n to 1
function minOperations( n)
{
    // To store the count of operations
    var count = 0;
 
    // To store the digit
    var d = 0;
 
    // If n is already then no
    // operation is required
    if (n == 1)
        return 0;
 
    // Extract all the digits except
    // the first digit
    while (n > 9) {
 
        // Store the maximum of that digits
        d = Math.max(n % 10, d);
        n /= 10;
 
        // for each digit
        count += 10;
    }
 
    // First digit
    d = Math.max(d, n - 1);
 
    // Add the value to count
    count += Math.abs(d);
 
    return count - 1;
}
 
var n = 240;
document.write(minOperations(n));
 
 
// This code is contributed by SoumikMondal
 
</script>
Output: 
23

 

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :