Given two integers **A** and **B**. the task is to find the minimum number of operations required to make A and B equal. In each operation, either of the below steps can be performed:

- Increment either A or B with its initial value.
- Increment both A and B with their initial value

**Examples:**

Input:A = 4, B = 10

Output:4

Explanation:

Initially A = 4, B = 10

Operation 1: Increment A only: A = A + 4 = 8

Operation 2: Increment A only: A = A + 4 = 12

Operation 3: Increment A only: A = A + 4 = 16

Operation 4: Increment A and B: A = A + 4 = 20 and B = B + 10 = 20

They are equal now.

Input:A = 7, B = 23

Output:22

Explanation:

Initially A = 7, B = 23

Operation 1 – 7: Increment A and B: A = 56 and B = 161

Operation 8 – 22: Increment A: A = 161 and B = 161

They are equal now.

**Approach:** This problem can be solved using GCD.

- If A is greater than B, then swap A and B.
- Now reduce B, such that gcd of A and B becomes 1.
- Hence the minimum operations required to reach equal value is (B – 1).

**For example:** If A = 4, B = 10:

*Step 1:*Compare 4 and 10, as we always need B as the greater value. Here already B is greater than A. So, now no swap is required.*Step 2:*GCD(4, 10) = 2. So, we reduce B to B/2. Now A = 4 and B = 5.

GCD(4, 5) = 1, which was the target.*Step 3:*(Current value of B – 1) will be the required count. Here, Current B = 5. So (5 – 1 = 4), i.e. total 4 operations are required.

Below is the implementation of the above approach.

## C++

`// C++ program to find minimum ` `// operations required to ` `// make two numbers equal ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the ` `// minimum operations required ` `long` `long` `int` `minOperations( ` ` ` `long` `long` `int` `A, ` ` ` `long` `long` `int` `B) ` `{ ` ` ` ` ` `// Keeping B always greater ` ` ` `if` `(A > B) ` ` ` `swap(A, B); ` ` ` ` ` `// Reduce B such that ` ` ` `// gcd(A, B) becomes 1. ` ` ` `B = B / __gcd(A, B); ` ` ` ` ` `return` `B - 1; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `long` `long` `int` `A = 7, B = 15; ` ` ` ` ` `cout << minOperations(A, B) ` ` ` `<< endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find minimum ` `// operations required to ` `// make two numbers equal ` `class` `GFG{ ` ` ` `// Function to return the ` `// minimum operations required ` `static` `int` `minOperations( ` ` ` `int` `A, ` ` ` `int` `B) ` `{ ` ` ` ` ` `// Keeping B always greater ` ` ` `if` `(A > B) { ` ` ` `A = A+B; ` ` ` `B = A-B; ` ` ` `A = A-B; ` ` ` `} ` ` ` ` ` `// Reduce B such that ` ` ` `// gcd(A, B) becomes 1. ` ` ` `B = B / __gcd(A, B); ` ` ` ` ` `return` `B - ` `1` `; ` `} ` `static` `int` `__gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `return` `b == ` `0` `? a:__gcd(b, a % b); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `A = ` `7` `, B = ` `15` `; ` ` ` ` ` `System.out.print(minOperations(A, B) ` ` ` `+` `"\n"` `); ` ` ` `} ` `} ` ` ` `// This code contributed by sapnasingh4991 ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to find minimum ` `# operations required to ` `# make two numbers equal ` `import` `math ` ` ` `# Function to return the ` `# minimum operations required ` `def` `minOperations(A, B): ` ` ` ` ` `# Keeping B always greater ` ` ` `if` `(A > B): ` ` ` `swap(A, B) ` ` ` ` ` `# Reduce B such that ` ` ` `# gcd(A, B) becomes 1. ` ` ` `B ` `=` `B ` `/` `/` `math.gcd(A, B); ` ` ` ` ` `return` `B ` `-` `1` ` ` `# Driver code ` `A ` `=` `7` `B ` `=` `15` ` ` `print` `(minOperations(A, B)) ` ` ` `# This code is contributed by Sanjit_Prasad ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find minimum ` `// operations required to ` `// make two numbers equal ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to return the ` `// minimum operations required ` `static` `int` `minOperations( ` ` ` `int` `A, ` ` ` `int` `B) ` `{ ` ` ` ` ` `// Keeping B always greater ` ` ` `if` `(A > B) { ` ` ` `A = A+B; ` ` ` `B = A-B; ` ` ` `A = A-B; ` ` ` `} ` ` ` ` ` `// Reduce B such that ` ` ` `// gcd(A, B) becomes 1. ` ` ` `B = B / __gcd(A, B); ` ` ` ` ` `return` `B - 1; ` `} ` `static` `int` `__gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `return` `b == 0? a:__gcd(b, a % b); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `A = 7, B = 15; ` ` ` ` ` `Console.Write(minOperations(A, B) ` ` ` `+` `"\n"` `); ` `} ` `} ` ` ` `// This code is contributed by sapnasingh4991 ` |

*chevron_right*

*filter_none*

**Output:**

14

**Time Complexity:** O(log(max(A, B))

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 operations to make two numbers equal
- Minimum number of cuts required to make circle segments equal sized
- Minimum deletions required to make GCD of the array equal to 1
- Count the number of carry operations required to add two numbers
- 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 number of operations required to reduce N to 0
- Minimum gcd operations to make all array elements one
- Minimum operations to make GCD of array a multiple of k
- Find minimum operations needed to make an Array beautiful
- Minimum cuts required to divide the Circle into equal parts
- Remove two consecutive integers from 1 to N to make sum equal to S
- Count the number of operations required to reduce the given number
- Split numbers from 1 to N into two equal sum subsets
- Append a digit in the end to make the number equal to the length of the remaining string
- Minimum sum of two numbers formed from digits of an array in O(n)
- Find Maximum and Minimum of two numbers using Absolute function
- Minimum insertions to make a Co-prime array
- Minimum removals from array to make GCD greater
- Find the minimum positive integer such that it is divisible by A and sum of its digits is equal to B

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.