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


We are given an integer B and A, and we have to convert the number K from 0 to exactly B by applying the given operations in the minimum steps.

  • We can increase the current number K by 1 i.e. K = K + 1

  • We can add the product of the number A with any power of 10 to the number K i.e. K = K + A * 10^p, where p is any non-negative number.

Sample Examples

Input

int A = 25
int B = 1337

Output

20

Explanation: We can reduce A * 10 five times from b and will get 1337 - 250 * 5 i.e. 87. Again, we can reduce 3 times a from current b and will get 12 which can be reduced to zero by subtracting one single time and will get 20 as the total number of steps.

Input

int A = 70
int B = 700

Output

1

Explanation: we can directly multiply a by 10 and reduce it from the b.

Approach 1

In this approach, we will first create a function that takes both a and b as the parameter and returns the required minimum number of steps as the return value.

In the function we will go from b to 0 to make it easy to calculate rather than going from making k 0 to b.

First, we will create a variable to store the steps and make k equal to a. We will use the while loop until b is greater than a. Then we make k equal to a and will multiply k by 10 until it becomes greater than b and then we will divide it by 10 to get the multiple of a which is just smaller than b and reduce it from b and maintain the count.

After the for loop ends we will have then b less than a and we can only subtract one from it and add the value to steps and return it to print it in the main function.

Example

#include <bits/stdc++.h>
using namespace std;

int getSteps(int a, int b){
   int k = 0;
   int steps = 0; // variable to store the steps 
   // getting the largest value of A * 10^p less than equals to B
   while(b >= a){
      k = a; // assigning k the value of a 
      while(k <= b){
         k *= 10;
      }      
      k /= 10; // removing the extra value of 10 factor 
      b -= k;
      steps++;
   }
   // now value of b is less than a so we just have to increment the k or decrease the b by 1 at each step
   steps += b; 
   return steps; // return the final answer 
}

int main(){
   int a = 25; // given number
   int b = 1337 ; // given target
   cout<<"The minimum number of steps required to convert K to B is "<<getSteps(a, b)<<endl;
   return 0; 
}

Output

The minimum number of steps required to convert K to B is 20

Time and Space Complexity

The time complexity of the above code is constant or O(1), because we are subracting the multiplication of the 10 from the given number means we need very less number of iterations even for the integer maximum value.

As we are not using any extra space here make the space complexity of the above code O(1) or constant.

Approach 2

This approach is slightly different from the previous approach and based on the mathematical concept that b = x * a + r, where x and r and the non-negative numbers, and we will reduce the factors of the a from the b to get the answer.

Example

#include <bits/stdc++.h>
using namespace std;

int getSteps(int a, int b){
   int k; // variable to work with later
   int steps = 0; // variable to store the steps 
   k = a; // assigning k value of a 
   // get the maximum multiple of a which is less than or equals to b
   while (k <= b) {
      k = k * 10;
   }
   // we need less or equal number 
   k /= 10;
   steps = b % a;
   // subtracting the value of steps from b as we are going to add one by one to achieve this also now b will be the multiple of a 
   b = b - b %a; 
   // reduce the k to make it less than a 
   while (k >= a) {
      steps = steps + b / k;
      b = b %k;
      k = k / 10;
   }
   return steps; // return the final answer 
}
int main(){
   int a = 25; // given number
   int b = 1337 ; // given target
   cout<<"The minimum number of steps required to convert K to B is "<<getSteps(a, b)<<endl;
   return 0; 
}

Output

The minimum number of steps required to convert K to B is 20

Time and Space Complexity

The time complexity of the above code is constant or O(1).

The space complexity of the above code is O(1), as we are not using any extra space here.

Conclusion

In this tutorial, we have implemented a program to get the minimum number of steps required to convert an integer k to b by performing given two tasks that are either incrementing the k by 1 or adding the given number 'A' multiplied by any positive power of 10 to k. We have implemented two approaches both with the constant time and space complexity by using the while loops.

Updated on: 31-Aug-2023

43 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements