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

**A = A + K****A = A – K**, where**K**belongs to**[1, 10]**

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 = 42Output:3Explanation:

The following sequence of moves can be performed: 13 → 23 → 32 → 42(add 10, add 9, add 10).

Input:A = 18, B = 4Output:2Explanation:

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:

- Initialize a variable
**required_moves**to store the minimum count of moves required. - Find the absolute difference of
**A**and**B**. - Iterate over the range
**[1, 10]**and perform the following operations:- Divide the number by
**i**and add it to the resultant variable. - Calculate modulo of absolute difference by
**i**.

- Divide the number by
- Finally, print the value of
**required_moves**.

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 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;` `}` |

*chevron_right*

*filter_none*

## Java

`// 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` |

*chevron_right*

*filter_none*

## Python3

`# 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` |

*chevron_right*

*filter_none*

## C#

`// 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` |

*chevron_right*

*filter_none*

**Output:**

19

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum number of moves required to reach the destination by the king in a chess board
- Minimum number of given moves required to make N divisible by 25
- Minimum moves required to type a word in QWERTY based keyboard
- Number of moves required to guess a permutation.
- Count of minimum reductions required to get the required sum K
- Count minimum number of moves to front or end to sort an array
- Maximum prime moves to convert X to Y
- Find minimum moves to reach target on an infinite line
- Minimum time to reach a point with +t and -t moves at time t
- Minimum number of moves to reach N starting from (1, 1)
- Minimum moves taken to move coin of each cell to any one cell of Matrix
- Find minimum moves to bring all elements in one cell of a matrix
- Minimum moves to reach target on a infinite line | Set 2
- Count possible moves in the given direction in a grid
- Count number of 1s in the array after N moves
- Minimum number of given operation required to convert n to m
- Minimum number operations required to convert n to m | Set-2
- Minimum operations required to convert X to Y by multiplying X with the given co-primes
- Minimum splits required to convert a number into prime segments
- Minimum flips required to convert given string into concatenation of equal substrings of length K

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.