Given two numbers A and B, the task is to find the maximum Greatest Common Divisors(GCD) that can be obtained by adding a number X to both A and B.
Examples
Input: A = 1, B = 5
Output: 4
Explanation: Adding X = 15, the obtained numbers are A = 16 and B = 20. Therefore, GCD of A, B is 4.Input: A = 2, B = 23
Output: 21
Approach: This problem can be solved in a very optimized manner using the properties of Euclidean GCD algorithm. Follow the steps below to solve the problem:
- If a > b:  GCD(a, b)= GCD(a – b, b). Therefore, GCD(a, b) = GCD(a – b, b).
- On adding x to A, B, gcd(a + x, b + x) = gcd(a – b, b + x). It can be seen that (a – b) remains constant.
- It can be safely said that the GCD of these numbers will never exceed (a – b). Since (b + x) can be made a multiple of (a – b) by adding a possible value of x.
- Therefore, it can be concluded that GCD remains (a – b).
Below is the implementation of the above approach.Â
C++
// C++ implementation of above approach.#include <iostream>using namespace std;Â
// Function to calculate maximum// gcd of two numbers possible by// adding same value to both a and bvoid maxGcd(int a, int b){Â Â Â Â cout << abs(a - b);}Â
// Driver Codeint main(){    // Given Input    int a = 2231;    int b = 343;Â
    maxGcd(a, b);Â
    return 0;} |
Java
// Java implementation of above approach.import java.io.*;Â
class GFG {Â
  // Function to calculate maximum  // gcd of two numbers possible by  // adding same value to both a and b  static void maxGcd(int a, int b)  {    System.out.println(Math.abs(a - b));  }Â
  // Driver Code  public static void main (String[] args)  {Â
    // Given Input    int a = 2231;    int b = 343;Â
    maxGcd(a, b);Â
  }}Â
// This code is contributed by Potta Lokesh |
Python3
# Python3 program for the above approachÂ
# Function to calculate maximum# gcd of two numbers possible by# adding same value to both a and bdef maxGcd(a, b):Â Â Â Â Â Â Â Â Â print(abs(a - b))Â
# Driver codeÂ
# Given Inputa = 2231b = 343Â
maxGcd(a, b)Â
# This code is contributed by Parth Manchanda |
C#
// C# program for the above approachusing System;Â
class GFG{Â
 // Function to calculate maximum  // gcd of two numbers possible by  // adding same value to both a and b  static void maxGcd(int a, int b)  {    Console.Write(Math.Abs(a - b));  }Â
// Driver Codestatic public void Main (){          // Given Input    int a = 2231;    int b = 343;Â
    maxGcd(a, b);}}Â
// This code is contributed by code_hunt. |
Javascript
<script>Â Â Â Â Â Â Â // JavaScript Program for the above approachÂ
       // Function to calculate maximum       // gcd of two numbers possible by       // adding same value to both a and b       function maxGcd(a, b) {           document.write(Math.abs(a - b));       }Â
       // Driver CodeÂ
       // Given Input       let a = 2231;       let b = 343;Â
       maxGcd(a, b);Â
   // This code is contributed by Potta Lokesh   </script> |
1888
Â
Time Complexity: O(1)
Auxiliary Space: O(1)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

… [Trackback]
[…] Information to that Topic: geeksforgeeks.org/maximum-gcd-of-two-numbers-possible-by-adding-same-value-to-them/ […]
… [Trackback]
[…] Find More Info here on that Topic: geeksforgeeks.org/maximum-gcd-of-two-numbers-possible-by-adding-same-value-to-them/ […]