Open In App

Minimize operations to convert K from 0 to B by adding 1 or A * 10^c in each step

Last Updated : 14 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given three numbers A, B, and K where K is 0 initially. The task is to find the minimum operations to convert K into B using the below operations:

  • Add 1 to K i.e K = K + 1
  • Add A * 10c in K i.e K = K + A * 10c, where c is any integer (c >= 0).

Examples:

Input: A = 2, B = 7
Output: 4
Explanation: Initially K = 0, following operations are done on K:

  1. K = K + A * 100 => K = 0 + 2 * 1 => K= 2
  2. K = K + A * 100 => K = 2 + 2 * 1 => K= 4
  3. K = K + A * 100 => K = 4 + 2 * 1 => K= 6
  4. Add 1 to K => K = 7

Therefore, minimum operations needed are 4

Input: A = 25, B = 1337
Output: 20

 

Approach: A number can be represented as B = X * A + Y, where A is the maximum number that is multiplied with X and their product is nearest to B and Y is that number that is less than A. Follow the below steps to solve the problem:

  • Initialize a variable K and assign X to it.
  • Multiply K with 10 until it is greater than B.
  • Initialize the variable ans with 0.
  • Store Y part in ans using modulus operator ans = B % A.
  • And subtract ans from B say B = B – ans.
  • Now module B by K until it is K is greater or equal to A.
  • And store division of B/K in ans.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the minimum operations
int minOperations(int A, int B)
{
 
    // Initialize K and assign A into K
    int K = A;
 
    // Calculate the nearest
    // smaller number with B
    while (K < B) {
        K = K * 10;
        // If K is larger than B divide
        // by 10 and break the loop
        // We can get the nearest number
        if (K > B) {
            K = K / 10;
            break;
        }
    }
 
    // Now according to approach
    // Y part is B % A
    // which is smaller than X
    int ans = B % A;
 
    // Subtract Y part from B
    B = B - ans;
 
    // Iterate until K is
    // Greater than or equal to A
    while (K >= A) {
 
        // store ans which is division number
        ans = ans + B / K;
 
        // Modulus B by K
        B = B % K;
 
        // Divide K by 10
        K = K / 10;
    }
    return ans;
}
 
// Driver Code
int main()
{
 
    int A = 25, B = 1337;
    int ans = minOperations(A, B);
    cout << ans;
    return 0;
}


Java




// Java code for the above approach
import java.util.*;
 
class GFG{
 
  // Function to find the minimum operations
  static int minOperations(int A, int B)
  {
 
    // Initialize K and assign A into K
    int K = A;
 
    // Calculate the nearest
    // smaller number with B
    while (K < B) {
      K = K * 10;
      // If K is larger than B divide
      // by 10 and break the loop
      // We can get the nearest number
      if (K > B) {
        K = K / 10;
        break;
      }
    }
 
    // Now according to approach
    // Y part is B % A
    // which is smaller than X
    int ans = B % A;
 
    // Subtract Y part from B
    B = B - ans;
 
    // Iterate until K is
    // Greater than or equal to A
    while (K >= A) {
 
      // store ans which is division number
      ans = ans + B / K;
 
      // Modulus B by K
      B = B % K;
 
      // Divide K by 10
      K = K / 10;
    }
    return ans;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int A = 25, B = 1337;
    int ans = minOperations(A, B);
    System.out.print(ans);
  }
}
 
// This code is contributed by sanjoy_62.


Python




# Python program for the above approach
 
# Function to find the minimum operations
def minOperations(A, B):
 
    # Initialize K and assign A into K
    K = A
 
    # Calculate the nearest
    # smaller number with B
    while (K < B):
         
        K = K * 10
        # If K is larger than B divide
        # by 10 and break the loop
        # We can get the nearest number
        if (K > B):
            K = K // 10
            break
 
    # Now according to approach
    # Y part is B % A
    # which is smaller than X
    ans = B % A
 
    # Subtract Y part from B
    B = B - ans
 
    # Iterate until K is
    # Greater than or equal to A
    while (K >= A):
 
        # store ans which is division number
        ans = ans + B // K
 
        # Modulus B by K
        B = B % K
 
        # Divide K by 10
        K = K // 10
 
    return ans
 
# Driver Code
A = 25
B = 1337
ans = minOperations(A, B)
print(ans)
    
# This code is contributed by Samim Hossain Mondal.


C#




// C# code for the above approach
using System;
class GFG
{
 
  // Function to find the minimum operations
  static int minOperations(int A, int B)
  {
 
    // Initialize K and assign A into K
    int K = A;
 
    // Calculate the nearest
    // smaller number with B
    while (K < B)
    {
      K = K * 10;
       
      // If K is larger than B divide
      // by 10 and break the loop
      // We can get the nearest number
      if (K > B)
      {
        K = K / 10;
        break;
      }
    }
 
    // Now according to approach
    // Y part is B % A
    // which is smaller than X
    int ans = B % A;
 
    // Subtract Y part from B
    B = B - ans;
 
    // Iterate until K is
    // Greater than or equal to A
    while (K >= A)
    {
 
      // store ans which is division number
      ans = ans + B / K;
 
      // Modulus B by K
      B = B % K;
 
      // Divide K by 10
      K = K / 10;
    }
    return ans;
  }
 
  // Driver Code
  public static void Main()
  {
    int A = 25, B = 1337;
    int ans = minOperations(A, B);
    Console.Write(ans);
  }
}
 
// This code is contributed by Saurabh Jaiswal


Javascript




<script>
    // JavaScript program for the above approach
 
    // Function to find the minimum operations
    const minOperations = (A, B) => {
 
        // Initialize K and assign A into K
        let K = A;
 
        // Calculate the nearest
        // smaller number with B
        while (K < B) {
            K = K * 10;
            // If K is larger than B divide
            // by 10 and break the loop
            // We can get the nearest number
            if (K > B) {
                K = parseInt(K / 10);
                break;
            }
        }
 
        // Now according to approach
        // Y part is B % A
        // which is smaller than X
        let ans = B % A;
 
        // Subtract Y part from B
        B = B - ans;
 
        // Iterate until K is
        // Greater than or equal to A
        while (K >= A) {
 
            // store ans which is division number
            ans = ans + parseInt(B / K);
 
            // Modulus B by K
            B = B % K;
 
            // Divide K by 10
            K = parseInt(K / 10);
        }
        return ans;
    }
 
    // Driver Code
    let A = 25, B = 1337;
    let ans = minOperations(A, B);
    document.write(ans);
 
// This code is contributed by rakeshsahni
 
</script>


Output

20

Time Complexity: O(1)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads