# Minimum number of increment / decrements required to be performed on one of the two given numbers to make them non-coprime

Given two positive integers **A** and **B**, the task is to find the minimum number of increments/decrements required to be performed on either **A** or **B** to make both the numbers **non-coprime**.

**Examples:**

Input:A = 12, B = 3Output:0Explanation:

As 12 & 3 are already non-coprimes, so the minimum count of increment/decrement operation required is 0.

Input:A = 7, B = 17Output:2

**Approach:** The given problem can be solved based on the following observations:

- If
**A**and**B**have Greatest Common Divisor greater than**1**then no increment or decrement is to be performed, as numbers are already non-coprime. - Now, check for the difference of
**1**in both directions for**A**as well as**B**. Hence it requires only a single step to convert any number to an even number. - If none of the above two cases applies then
**2**increments/decrements operations are required to make the numbers**A**and**B**to their nearest even number so that the numbers become non-co primes.

Based on the above observations, follow the steps below to solve the problem:

- If the GCD of
**A**and**B**is not equal to**1**, then print**0**as no operation is required. - Else if the GCD of one of the pair {
**{A + 1, B}**,**{A – 1, B}**,**{A, B + 1}**,**{A, B – 1}**} is not equal to**1**, then print**1**as only one operations is required. - Otherwise, print
**2**.

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 the minimum number of` `// increments/decrements operations required` `// to make both the numbers non-coprime` `int` `makeCoprime(` `int` `a, ` `int` `b)` `{` ` ` `// If a & b are already non-coprimes` ` ` `if` `(__gcd(a, b) != 1)` ` ` `return` `0;` ` ` `// If a & b can become non-coprimes` ` ` `// by only incrementing/decrementing` ` ` `// a number only once` ` ` `if` `(__gcd(a - 1, b) != 1` ` ` `or __gcd(a + 1, b) != 1` ` ` `or __gcd(b - 1, a) != 1` ` ` `or __gcd(b + 1, a) != 1)` ` ` `return` `1;` ` ` `// Otherwise` ` ` `return` `2;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `A = 7, B = 17;` ` ` `cout << makeCoprime(A, B);` ` ` `return` `0;` `}` |

## Java

`// Java program for the above approach` `public` `class` `GFG` `{` ` ` `// function to calculate gcd` ` ` `static` `int` `__gcd(` `int` `a, ` `int` `b)` ` ` `{` ` ` ` ` `// Everything divides 0` ` ` `if` `(a == ` `0` `)` ` ` `return` `b;` ` ` `if` `(b == ` `0` `)` ` ` `return` `a;` ` ` `// base case` ` ` `if` `(a == b)` ` ` `return` `a;` ` ` `// a is greater` ` ` `if` `(a > b)` ` ` `return` `__gcd(a-b, b);` ` ` `return` `__gcd(a, b-a);` ` ` `}` ` ` `// Function to find the minimum number of` ` ` `// increments/decrements operations required` ` ` `// to make both the numbers non-coprime` ` ` `static` `int` `makeCoprime(` `int` `a, ` `int` `b)` ` ` `{` ` ` `// If a & b are already non-coprimes` ` ` `if` `(__gcd(a, b) != ` `1` `)` ` ` `return` `0` `;` ` ` `// If a & b can become non-coprimes` ` ` `// by only incrementing/decrementing` ` ` `// a number only once` ` ` `if` `(__gcd(a - ` `1` `, b) != ` `1` `|| __gcd(a + ` `1` `, b) != ` `1` ` ` `|| __gcd(b - ` `1` `, a) != ` `1` ` ` `|| __gcd(b + ` `1` `, a) != ` `1` `)` ` ` `return` `1` `;` ` ` `// Otherwise` ` ` `return` `2` `;` ` ` `}` ` ` `// Driver code` ` ` `public` `static` `void` `main(String args[])` ` ` `{` ` ` `int` `A = ` `7` `, B = ` `17` `;` ` ` `System.out.println(makeCoprime(A, B));` ` ` `}` `}` `// This code is contributed by SoumikMondal` |

## Python3

`# Python3 program for the above approach` `from` `math ` `import` `gcd` `# Function to find the minimum number of` `# increments/decrements operations required` `# to make both the numbers non-coprime` `def` `makeCoprime(a, b):` ` ` ` ` `# If a & b are already non-coprimes` ` ` `if` `(gcd(a, b) !` `=` `1` `):` ` ` `return` `0` ` ` `# If a & b can become non-coprimes` ` ` `# by only incrementing/decrementing` ` ` `# a number only once` ` ` `if` `(gcd(a ` `-` `1` `, b) !` `=` `1` `or` ` ` `gcd(a ` `+` `1` `, b) !` `=` `1` `or` ` ` `gcd(b ` `-` `1` `, a) !` `=` `1` `or` ` ` `gcd(b ` `+` `1` `, a) !` `=` `1` `):` ` ` `return` `1` ` ` `# Otherwise` ` ` `return` `2` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` ` ` `A ` `=` `7` ` ` `B ` `=` `17` ` ` ` ` `print` `(makeCoprime(A, B))` `# This code is contributed by SURENDRA_GANGWAR` |

## C#

`// C# program for the above approach` `using` `System;` `class` `GFG{` `// Function to calculate gcd` `static` `int` `__gcd(` `int` `a, ` `int` `b)` `{` ` ` ` ` `// Everything divides 0` ` ` `if` `(a == 0)` ` ` `return` `b;` ` ` `if` `(b == 0)` ` ` `return` `a;` ` ` ` ` `// Base case` ` ` `if` `(a == b)` ` ` `return` `a;` ` ` ` ` `// a is greater` ` ` `if` `(a > b)` ` ` `return` `__gcd(a - b, b);` ` ` ` ` `return` `__gcd(a, b - a);` `}` `// Function to find the minimum number of` `// increments/decrements operations required` `// to make both the numbers non-coprime` `static` `int` `makeCoprime(` `int` `a, ` `int` `b)` `{` ` ` ` ` `// If a & b are already non-coprimes` ` ` `if` `(__gcd(a, b) != 1)` ` ` `return` `0;` ` ` ` ` `// If a & b can become non-coprimes` ` ` `// by only incrementing/decrementing` ` ` `// a number only once` ` ` `if` `(__gcd(a - 1, b) != 1 ||` ` ` `__gcd(a + 1, b) != 1 ||` ` ` `__gcd(b - 1, a) != 1 ||` ` ` `__gcd(b + 1, a) != 1)` ` ` `return` `1;` ` ` ` ` `// Otherwise` ` ` `return` `2;` `}` `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` ` ` `int` `A = 7, B = 17;` ` ` ` ` `Console.Write(makeCoprime(A, B));` `}` `}` `// This code is contributed by sanjoy_62` |

## Javascript

`<script>` ` ` `// JavaScript program for the above approach` ` ` `function` `__gcd(a, b) {` ` ` `if` `(b == 0)` ` ` `return` `a;` ` ` `return` `__gcd(b, a % b);` ` ` `}` ` ` `// Function to find the minimum number of` ` ` `// increments/decrements operations required` ` ` `// to make both the numbers non-coprime` ` ` `function` `makeCoprime(a, b) {` ` ` `// If a & b are already non-coprimes` ` ` `if` `(__gcd(a, b) != 1)` ` ` `return` `0;` ` ` `// If a & b can become non-coprimes` ` ` `// by only incrementing/decrementing` ` ` `// a number only once` ` ` `if` `(__gcd(a - 1, b) != 1` ` ` `|| __gcd(a + 1, b) != 1` ` ` `|| __gcd(b - 1, a) != 1` ` ` `|| __gcd(b + 1, a) != 1)` ` ` `return` `1;` ` ` `// Otherwise` ` ` `return` `2;` ` ` `}` ` ` `// Driver Code` ` ` `let A = 7, B = 17;` ` ` `document.write(makeCoprime(A, B));` ` ` `// This code is contributed by Potta Lokesh` ` ` `</script>` |

**Output:**

2

**Time Complexity:** O(log(A, B))**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**