Thursday, October 10, 2024
Google search engine
HomeData Modelling & AIMinimize operations to convert K from 0 to B by adding 1...

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

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)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Commit to GfG’s Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

Last Updated :
14 Apr, 2023
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

Share your thoughts in the comments

RELATED ARTICLES

Most Popular

Recent Comments