# Minimize operations to reduce A and B to 1 by decrementing 1 or dividing A by B and B by A

• Difficulty Level : Expert
• Last Updated : 26 Oct, 2021

Given two positive integers A and B. The task is to minimize operations required to reduce A and B to 1. There are two types of operation:

1. Decrement either A or B by 1.
2. Divide A by B or B by A or perform both divisions simultaneously only if the remainder on division is 0.

Example:

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: A = 13, B = 5
Output: 6
Explanation: Below are the operations performed to reduce A and B to 1.

Initially A = 13, B = 5
Step 1: Decrement A by 1: A = 12, B = 5
Step 2: Decrement B by 1: A = 12, B = 4
Step 3: Divide A by B and assign it to A : A = 3, B = 4
Step 4: Decrement A by 1: A = 2, B = 4
Step 5: Divide B by A and assign it to B : A = 2, B = 2
Step 6: Divide both A by B and B by A, assign it to A and B respectively: A = 1, B = 1
Therefore, total 6 steps required to reduce A and B to 1. Which is minimum possible.

Input: A = 3, B = 27
Output: 3

Approach: This problem can be solved by using recursion. Create a recursive and Take a variable say cntOperations = 0 to keep track of the number of operations performed. For base condition check if A=1 and B=1, then return cntOperations otherwise, check for each possible operation that can be performed.

Below is the implementation of the above approach:

## C++

 `// C++ program for above approach``#include ``using` `namespace` `std;` `// Recursive function to find minimum operation``// required to reduce A and B to 1``int` `solve(``int` `A, ``int` `B, ``int` `ans = 0)``{``    ``// Base Condition: When A and B reduced to 1``    ``if` `(A == 1 && B == 1) {``        ``return` `ans;``    ``}` `    ``// If A and B are equal``    ``if` `(A % B == 0 && B % A == 0) {``        ``return` `solve(A / B, B / A, ans + 1);``    ``}` `    ``// If A is divisible by B``    ``else` `if` `(A % B == 0 && B % A != 0) {``        ``return` `solve(A / B, B, ans + 1);``    ``}` `    ``// If B is divisible by A``    ``else` `if` `(A % B != 0 && B % A == 0) {``        ``return` `solve(A, B / A, ans + 1);``    ``}` `    ``// If A-1 is even``    ``else` `if` `((A - 1) % 2 == 0) {``        ``return` `solve(A - 1, B, ans + 1);``    ``}` `    ``// If B-1 is even``    ``else` `if` `((B - 1) % 2 == 0) {``        ``return` `solve(A, B - 1, ans + 1);``    ``}` `    ``else` `{` `        ``// If A is less than B``        ``if` `(A < B) {``            ``return` `solve(A - 1, B, ans + 1);``        ``}` `        ``// If B is less than A``        ``else` `{``            ``return` `solve(A, B - 1, ans + 1);``        ``}``    ``}``}` `// Driver Code``int` `main()``{``    ``int` `A = 13;``    ``int` `B = 5;` `    ``cout << solve(A, B);``}`

## Java

 `// Java program for above approach``class` `GFG``{``  ` `    ``// Recursive function to find minimum operation``    ``// required to reduce A and B to 1``    ``public` `static` `int` `solve(``int` `A, ``int` `B, ``int` `ans)``    ``{``      ` `        ``// Base Condition: When A and B reduced to 1``        ``if` `(A == ``1` `&& B == ``1``) {``            ``return` `ans;``        ``}` `        ``// If A and B are equal``        ``if` `(A % B == ``0` `&& B % A == ``0``) {``            ``return` `solve(A / B, B / A, ans + ``1``);``        ``}` `        ``// If A is divisible by B``        ``else` `if` `(A % B == ``0` `&& B % A != ``0``) {``            ``return` `solve(A / B, B, ans + ``1``);``        ``}` `        ``// If B is divisible by A``        ``else` `if` `(A % B != ``0` `&& B % A == ``0``) {``            ``return` `solve(A, B / A, ans + ``1``);``        ``}` `        ``// If A-1 is even``        ``else` `if` `((A - ``1``) % ``2` `== ``0``) {``            ``return` `solve(A - ``1``, B, ans + ``1``);``        ``}` `        ``// If B-1 is even``        ``else` `if` `((B - ``1``) % ``2` `== ``0``) {``            ``return` `solve(A, B - ``1``, ans + ``1``);``        ``}` `        ``else` `{` `            ``// If A is less than B``            ``if` `(A < B) {``                ``return` `solve(A - ``1``, B, ans + ``1``);``            ``}` `            ``// If B is less than A``            ``else` `{``                ``return` `solve(A, B - ``1``, ans + ``1``);``            ``}``        ``}``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String args[]) {``        ``int` `A = ``13``;``        ``int` `B = ``5``;` `        ``System.out.println(solve(A, B, ``0``));``    ``}``}` `// This code is contributed by gfgking,`

## Python3

 `# python program for above approach` `# Recursive function to find minimum operation``# required to reduce A and B to 1``def` `solve(A, B, ans``=``0``):` `    ``# Base Condition: When A and B reduced to 1``    ``if` `(A ``=``=` `1` `and` `B ``=``=` `1``):``        ``return` `ans` `    ``# If A and B are equal``    ``if` `(A ``%` `B ``=``=` `0` `and` `B ``%` `A ``=``=` `0``):``        ``return` `solve(A ``/``/` `B, B ``/``/` `A, ans ``+` `1``)` `    ``# If A is divisible by B``    ``elif` `(A ``%` `B ``=``=` `0` `and` `B ``%` `A !``=` `0``):``        ``return` `solve(A ``/``/` `B, B, ans ``+` `1``)` `    ``# If B is divisible by A``    ``elif` `(A ``%` `B !``=` `0` `and` `B ``%` `A ``=``=` `0``):``        ``return` `solve(A, B ``/``/` `A, ans ``+` `1``)` `    ``# If A-1 is even``    ``elif` `((A ``-` `1``) ``%` `2` `=``=` `0``):``        ``return` `solve(A ``-` `1``, B, ans ``+` `1``)` `    ``# If B-1 is even``    ``elif` `((B ``-` `1``) ``%` `2` `=``=` `0``):``        ``return` `solve(A, B ``-` `1``, ans ``+` `1``)` `    ``else``:` `        ``# If A is less than B``        ``if` `(A < B):``            ``return` `solve(A ``-` `1``, B, ans ``+` `1``)` `        ``# If B is less than A``        ``else``:``            ``return` `solve(A, B ``-` `1``, ans ``+` `1``)` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:` `    ``A ``=` `13``    ``B ``=` `5` `    ``print``(solve(A, B))` `# This code is contributed by rakeshsahni`

## C#

 `// C# program for the above approach``using` `System;``public` `class` `GFG``{``  ` `    ``// Recursive function to find minimum operation``    ``// required to reduce A and B to 1``    ``static` `int` `solve(``int` `A, ``int` `B, ``int` `ans)``    ``{` `        ``// Base Condition: When A and B reduced to 1``        ``if` `(A == 1 && B == 1) {``            ``return` `ans;``        ``}` `        ``// If A and B are equal``        ``if` `(A % B == 0 && B % A == 0) {``            ``return` `solve(A / B, B / A, ans + 1);``        ``}` `        ``// If A is divisible by B``        ``else` `if` `(A % B == 0 && B % A != 0) {``            ``return` `solve(A / B, B, ans + 1);``        ``}` `        ``// If B is divisible by A``        ``else` `if` `(A % B != 0 && B % A == 0) {``            ``return` `solve(A, B / A, ans + 1);``        ``}` `        ``// If A-1 is even``        ``else` `if` `((A - 1) % 2 == 0) {``            ``return` `solve(A - 1, B, ans + 1);``        ``}` `        ``// If B-1 is even``        ``else` `if` `((B - 1) % 2 == 0) {``            ``return` `solve(A, B - 1, ans + 1);``        ``}` `        ``else` `{` `            ``// If A is less than B``            ``if` `(A < B) {``                ``return` `solve(A - 1, B, ans + 1);``            ``}` `            ``// If B is less than A``            ``else` `{``                ``return` `solve(A, B - 1, ans + 1);``            ``}``        ``}``    ``}` `    ``// Driver Code``    ``public` `static` `void` `Main(String[] args)``    ``{``        ``int` `A = 13;``        ``int` `B = 5;``        ``Console.WriteLine(solve(A, B, 0));``    ``}``}` `// This code is contributed by dwivediyash`

## Javascript

 ``
Output
`6`

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

Auxiliary Space: O(1)

My Personal Notes arrow_drop_up