Given two integers **A** and **B**, the task is to find the minimum number of operations required to change this pair to (1, 1). In each operation (A, B) can be changed to (A – B, B) where A > B.

**Note:** If there is no possible solution to reach (1, 1), print -1.

**Examples:**

Input:A = 7, B = 8

Output:7

Explanation:

Operation 1: (A, B) => (7, 8) => (7, 1)

Operation 2: (A, B) => (7, 1) => (6, 1)

Operation 3: (A, B) => (6, 1) => (5, 1)

Operation 4: (A, B) => (5, 1) => (4, 1)

Operation 5: (A, B) => (4, 1) => (3, 1)

Operation 6: (A, B) => (3, 1) => (2, 1)

Operation 7: (A, B) => (2, 1) => (1, 1)

Input:A = 75, B = 17

Output:10

**Naive Approach:** The idea is to use recursion and update the pair as (A, A – B), where A > B and increase the number of operations required by 1. If at any step, any element of the pair is less than 1 then it is not possible to reach (1, 1).

Below is the implementation of the above approach:

## C++

`// C++ implementation to find the ` `// minimum number of operations ` `// required to reach (1, 1) ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the minimum ` `// number of steps required ` `int` `minimumSteps(` `int` `a, ` `int` `b, ` `int` `c) ` `{ ` ` ` ` ` `// Conditon to check if it ` ` ` `// is not possible to reach ` ` ` `if` `(a < 1 || b < 1) ` ` ` `return` `-1; ` ` ` ` ` `// Condition to check if the ` ` ` `// pair is reached to 1, 1 ` ` ` `if` `(a == 1 && b == 1) ` ` ` `return` `c; ` ` ` ` ` `// Condition to change the ` ` ` `// A as the maximum element ` ` ` `if` `(a < b) ` ` ` `{ ` ` ` `a = a + b; ` ` ` `b = a - b; ` ` ` `a = a - b; ` ` ` `} ` ` ` ` ` `return` `minimumSteps(a - b, b, c + 1); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `a = 75; ` ` ` `int` `b = 17; ` ` ` ` ` `cout << minimumSteps(a, b, 0) << endl; ` `} ` ` ` `// This code is contributed by AbhiThakur ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find the ` `// minimum number of operations ` `// required to reach (1, 1) ` `class` `GFG{ ` ` ` `// Function to find the minimum ` `// number of steps required ` `static` `int` `minimumSteps(` `int` `a, ` `int` `b, ` `int` `c) ` `{ ` ` ` ` ` `// Conditon to check if it ` ` ` `// is not possible to reach ` ` ` `if` `(a < ` `1` `|| b < ` `1` `) ` ` ` `return` `-` `1` `; ` ` ` ` ` `// Condition to check if the ` ` ` `// pair is reached to 1, 1 ` ` ` `if` `(a == ` `1` `&& b == ` `1` `) ` ` ` `return` `c; ` ` ` ` ` `// Condition to change the ` ` ` `// A as the maximum element ` ` ` `if` `(a < b) ` ` ` `{ ` ` ` `a = a + b; ` ` ` `b = a - b; ` ` ` `a = a - b; ` ` ` `} ` ` ` ` ` `return` `minimumSteps(a - b, b, c + ` `1` `); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String []args) ` `{ ` ` ` `int` `a = ` `75` `; ` ` ` `int` `b = ` `17` `; ` ` ` ` ` `System.out.println(minimumSteps(a, b, ` `0` `)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to find the ` `# minimum number of operations ` `# required to reach (1, 1) ` ` ` `# Function to find the minimum ` `# number of steps required ` `def` `minimumSteps(a, b, c): ` ` ` ` ` `# Conditon to check if it ` ` ` `# is not possible to reach ` ` ` `if` `a < ` `1` `or` `b < ` `1` `: ` ` ` `return` `-` `1` ` ` ` ` `# Condition to check if the ` ` ` `# pair is reached to 1, 1 ` ` ` `if` `a ` `=` `=` `1` `and` `b ` `=` `=` `1` `: ` ` ` `return` `c ` ` ` ` ` `# Condition to change the ` ` ` `# A as the maximum element ` ` ` `if` `a < b: ` ` ` `a, b ` `=` `b, a ` ` ` ` ` `return` `minimumSteps(a` `-` `b, b, c ` `+` `1` `) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `a ` `=` `75` `; b ` `=` `17` ` ` ` ` `print` `(minimumSteps(a, b, ` `0` `)) ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to find the ` `// minimum number of operations ` `// required to reach (1, 1) ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to find the minimum ` `// number of steps required ` `static` `int` `minimumSteps(` `int` `a, ` `int` `b, ` `int` `c) ` `{ ` ` ` ` ` `// Conditon to check if it ` ` ` `// is not possible to reach ` ` ` `if` `(a < 1 || b < 1) ` ` ` `return` `-1; ` ` ` ` ` `// Condition to check if the ` ` ` `// pair is reached to 1, 1 ` ` ` `if` `(a == 1 && b == 1) ` ` ` `return` `c; ` ` ` ` ` `// Condition to change the ` ` ` `// A as the maximum element ` ` ` `if` `(a < b) ` ` ` `{ ` ` ` `a = a + b; ` ` ` `b = a - b; ` ` ` `a = a - b; ` ` ` `} ` ` ` `return` `minimumSteps(a - b, b, c + 1); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `a = 75; ` ` ` `int` `b = 17; ` ` ` ` ` `Console.WriteLine(minimumSteps(a, b, 0)); ` `} ` `} ` ` ` `// This code is contributed by AbhiThakur ` |

*chevron_right*

*filter_none*

**Output:**

10

**Effiecient Approach:** The idea is to use Euclidean algorithm to solve this problem. By this approach, we can go **from (A, B) to (A % B, B)** in **A/B** steps. But if the minimum of the A and B is 1, then we can reach (1, 1) in **A – 1** steps.

Below is the implementation of the above approach:

## C++

`// C++ implementation to find the ` `// minimum number of operations ` `// required to reach (1, 1) ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the minimum ` `int` `minimumSteps(` `int` `a, ` `int` `b, ` `int` `c) ` `{ ` ` ` ` ` `// Conditon to check if it ` ` ` `// is not possible to reach ` ` ` `if` `(a < 1 || b < 1) ` ` ` `{ ` ` ` `return` `-1; ` ` ` `} ` ` ` ` ` `// Condition to check if the ` ` ` `// pair is reached to 1, 1 ` ` ` `if` `(min(a, b) == 1) ` ` ` `{ ` ` ` `return` `c + max(a, b) - 1; ` ` ` `} ` ` ` ` ` `// Condition to change the ` ` ` `// A as the maximum element ` ` ` `if` `(a < b) ` ` ` `{ ` ` ` `swap(a, b); ` ` ` `} ` ` ` ` ` `return` `minimumSteps(a % b, b, c + (a / b)); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `a = 75, b = 17; ` ` ` `cout << minimumSteps(a, b, 0) << endl; ` ` ` ` ` `return` `0; ` `} ` ` ` `// This code is contributed by rutvik_56 ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find the ` `// minimum number of operations ` `// required to reach (1, 1) ` `import` `java.util.*; ` `class` `GFG{ ` ` ` `// Function to find the minimum ` `static` `int` `minimumSteps(` `int` `a, ` `int` `b, ` `int` `c) ` `{ ` ` ` ` ` `// Conditon to check if it ` ` ` `// is not possible to reach ` ` ` `if` `(a < ` `1` `|| b < ` `1` `) ` ` ` `{ ` ` ` `return` `-` `1` `; ` ` ` `} ` ` ` ` ` `// Condition to check if the ` ` ` `// pair is reached to 1, 1 ` ` ` `if` `(Math.min(a, b) == ` `1` `) ` ` ` `{ ` ` ` `return` `c + Math.max(a, b) - ` `1` `; ` ` ` `} ` ` ` ` ` `// Condition to change the ` ` ` `// A as the maximum element ` ` ` `if` `(a < b) ` ` ` `{ ` ` ` `a = a + b; ` ` ` `b = a - b; ` ` ` `a = a - b; ` ` ` `} ` ` ` ` ` `return` `minimumSteps(a % b, b, c + (a / b)); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `a = ` `75` `, b = ` `17` `; ` ` ` `System.out.print( ` ` ` `minimumSteps(a, b, ` `0` `) + ` `"\n"` `); ` `} ` `} ` ` ` `// This code is contributed by sapnasingh4991 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to find the ` `# minimum number of operations ` `# required to reach (1, 1) ` ` ` `# Function to find the minimum ` `# number of steps required ` `def` `minimumSteps(a, b, c): ` ` ` ` ` `# Conditon to check if it ` ` ` `# is not possible to reach ` ` ` `if` `a < ` `1` `or` `b < ` `1` `: ` ` ` `return` `-` `1` ` ` ` ` `# Condition to check if the ` ` ` `# pair is reached to 1, 1 ` ` ` `if` `min` `(a, b) ` `=` `=` `1` `: ` ` ` `return` `c ` `+` `max` `(a, b) ` `-` `1` ` ` ` ` `# Condition to change the ` ` ` `# A as the maximum element ` ` ` `if` `a < b: ` ` ` `a, b ` `=` `b, a ` ` ` ` ` `return` `minimumSteps(a ` `%` `b, b, c ` `+` `a` `/` `/` `b) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `a ` `=` `75` `; b ` `=` `17` ` ` ` ` `print` `(minimumSteps(a, b, ` `0` `)) ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to find the ` `// minimum number of operations ` `// required to reach (1, 1) ` `using` `System; ` `class` `GFG{ ` ` ` `// Function to find the minimum ` `static` `int` `minimumSteps(` `int` `a, ` `int` `b, ` `int` `c) ` `{ ` ` ` ` ` `// Conditon to check if it ` ` ` `// is not possible to reach ` ` ` `if` `(a < 1 || b < 1) ` ` ` `{ ` ` ` `return` `-1; ` ` ` `} ` ` ` ` ` `// Condition to check if the ` ` ` `// pair is reached to 1, 1 ` ` ` `if` `(Math.Min(a, b) == 1) ` ` ` `{ ` ` ` `return` `c + Math.Max(a, b) - 1; ` ` ` `} ` ` ` ` ` `// Condition to change the ` ` ` `// A as the maximum element ` ` ` `if` `(a < b) ` ` ` `{ ` ` ` `a = a + b; ` ` ` `b = a - b; ` ` ` `a = a - b; ` ` ` `} ` ` ` ` ` `return` `minimumSteps(a % b, b, c + (a / b)); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `a = 75, b = 17; ` ` ` `Console.Write(minimumSteps(a, b, 0) + ` `"\n"` `); ` `} ` `} ` ` ` `// This code is contributed by Nidhi_biet ` |

*chevron_right*

*filter_none*

**Output:**

10

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:

- Minimize Sum of an Array by at most K reductions
- Count of minimum reductions required to get the required sum K
- Number of ways to divide string in sub-strings such to make them in lexicographically increasing sequence
- Make A, B and C equal by adding total value N to them
- Minimum value to be added to the prefix sums at each array indices to make them positive
- Delete continuous nodes with sum K from a given linked list
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Minimum number of integers required such that each Segment contains at least one of them
- GCD of two numbers when one of them can be very large
- Reach A and B by multiplying them with K and K^2 at every step
- Check whether it is possible to join two points given on circle such that distance between them is k
- Count numbers < = N whose difference with the count of primes upto them is > = K
- Count pairs in an array such that the absolute difference between them is ≥ K
- Count the pairs in an array such that the difference between them and their indices is equal
- Find two numbers with sum N such that neither of them contains digit K
- Largest possible value of M not exceeding N having equal Bitwise OR and XOR between them
- Split N into two integers whose addition to A and B makes them equal
- Maximise the sum of two Numbers using at most one swap between them
- Sort numbers based on count of letters required to represent them in words
- Find minimum number to be divided to make a number a perfect square

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.