Open In App

Count minimum moves required to convert A to B

Given two integers A and B, convert A to B by performing one of the following operations any number of times:

The task is to find the minimum number of operations required to convert A to B using the above operations.

Examples:

Input: A = 13, B = 42
Output: 3
Explanation:
The following sequence of moves can be performed: 13 ? 23 ? 32 ? 42(add 10, add 9, add 10).

Input: A = 18, B = 4
Output: 2
Explanation:
The following sequence of moves can be performed: 18 ? 10 ? 4 (subtract 8, subtract 6).

Approach: The idea is to simply calculate the required number of moves by dividing the absolute difference of A and B by all the numbers in the range [1…10] and adding it to the resultant variable. Follow the steps below to solve the problem:

Below is the implementation of the above approach:




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find minimum number
// of moves to obtained B from A
void convertBfromA(int a, int b)
{
    // Stores the minimum
    // number of moves
    int moves = 0;
 
    // Absolute difference
    int x = abs(a - b);
 
    // K is in range [0, 10]
    for (int i = 10; i > 0; i--) {
        moves += x / i;
        x = x % i;
    }
 
    // Print the required moves
    cout << moves << " ";
}
 
// Driver Code
int main()
{
    int A = 188, B = 4;
 
    convertBfromA(A, B);
 
    return 0;
}




// Java program for the above approach
import java.io.*;
 
class GFG{
 
// Function to find minimum number
// of moves to obtained B from A
static void convertBfromA(int a, int b)
{
     
    // Stores the minimum
    // number of moves
    int moves = 0;
 
    // Absolute difference
    int x = Math.abs(a - b);
 
    // K is in range [0, 10]
    for(int i = 10; i > 0; i--)
    {
        moves += x / i;
        x = x % i;
    }
 
    // Print the required moves
    System.out.print(moves + " ");
}
 
// Driver Code
public static void main (String[] args)
{
    int A = 188, B = 4;
 
    convertBfromA(A, B);
}
}
 
// This code is contributed by code_hunt




# Python3 program for the above approach
 
# Function to find minimum number
# of moves to obtained B from A
def convertBfromA(a, b):
     
    # Stores the minimum
    # number of moves
    moves = 0
 
    # Absolute difference
    x = abs(a - b)
 
    # K is in range [0, 10]
    for i in range(10, 0, -1):
        moves += x // i
        x = x % i
     
    # Print the required moves
    print(moves, end = " ")
 
# Driver Code
A = 188
B = 4
 
convertBfromA(A, B)
 
# This code is contributed by code_hunt




// C# program for the above approach 
using System;
 
class GFG{
 
// Function to find minimum number
// of moves to obtained B from A
static void convertBfromA(int a, int b)
{
     
    // Stores the minimum
    // number of moves
    int moves = 0;
 
    // Absolute difference
    int x = Math.Abs(a - b);
 
    // K is in range [0, 10]
    for(int i = 10; i > 0; i--)
    {
        moves += x / i;
        x = x % i;
    }
 
    // Print the required moves
    Console.Write(moves + " ");
}
 
// Driver Code
public static void Main ()
{
    int A = 188, B = 4;
 
    convertBfromA(A, B);
}
}
 
// This code is contributed by code_hunt




<script>
 
// Javascript program to implement
// the above approach
 
// Function to find minimum number
// of moves to obtained B from A
function convertBfromA(a, b)
{
      
    // Stores the minimum
    // number of moves
    let moves = 0;
  
    // Absolute difference
    let x = Math.abs(a - b);
  
    // K is in range [0, 10]
    for(let i = 10; i > 0; i--)
    {
        moves += Math.floor(x / i);
        x = x % i;
    }
  
    // Print the required moves
    document.write(moves + " ");
}
 
// Driver Code
 
    let A = 188, B = 4;
  
    convertBfromA(A, B);
                 
</script>

Output: 
19

 

Time Complexity: O(K), where K is in the range [0, 10]
Auxiliary Space: O(1)


Article Tags :