Minimize operations to convert K from 0 to B by adding 1 or A * 10^c in each step
Last Updated :
14 Apr, 2023
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:
- K = K + A * 100 => K = 0 + 2 * 1 => K= 2
- K = K + A * 100 => K = 2 + 2 * 1 => K= 4
- K = K + A * 100 => K = 4 + 2 * 1 => K= 6
- 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++
#include <bits/stdc++.h>
using namespace std;
int minOperations( int A, int B)
{
int K = A;
while (K < B) {
K = K * 10;
if (K > B) {
K = K / 10;
break ;
}
}
int ans = B % A;
B = B - ans;
while (K >= A) {
ans = ans + B / K;
B = B % K;
K = K / 10;
}
return ans;
}
int main()
{
int A = 25, B = 1337;
int ans = minOperations(A, B);
cout << ans;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int minOperations( int A, int B)
{
int K = A;
while (K < B) {
K = K * 10 ;
if (K > B) {
K = K / 10 ;
break ;
}
}
int ans = B % A;
B = B - ans;
while (K >= A) {
ans = ans + B / K;
B = B % K;
K = K / 10 ;
}
return ans;
}
public static void main(String[] args)
{
int A = 25 , B = 1337 ;
int ans = minOperations(A, B);
System.out.print(ans);
}
}
|
Python
def minOperations(A, B):
K = A
while (K < B):
K = K * 10
if (K > B):
K = K / / 10
break
ans = B % A
B = B - ans
while (K > = A):
ans = ans + B / / K
B = B % K
K = K / / 10
return ans
A = 25
B = 1337
ans = minOperations(A, B)
print (ans)
|
C#
using System;
class GFG
{
static int minOperations( int A, int B)
{
int K = A;
while (K < B)
{
K = K * 10;
if (K > B)
{
K = K / 10;
break ;
}
}
int ans = B % A;
B = B - ans;
while (K >= A)
{
ans = ans + B / K;
B = B % K;
K = K / 10;
}
return ans;
}
public static void Main()
{
int A = 25, B = 1337;
int ans = minOperations(A, B);
Console.Write(ans);
}
}
|
Javascript
<script>
const minOperations = (A, B) => {
let K = A;
while (K < B) {
K = K * 10;
if (K > B) {
K = parseInt(K / 10);
break ;
}
}
let ans = B % A;
B = B - ans;
while (K >= A) {
ans = ans + parseInt(B / K);
B = B % K;
K = parseInt(K / 10);
}
return ans;
}
let A = 25, B = 1337;
let ans = minOperations(A, B);
document.write(ans);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...