Find minimum sum Pair whose GCD is greater than 1
Given two positive integers A and B, the task is to find two positive integers such that they belong in the range [A, B], such that their GCD is greater than 1 and their sum is the minimum among all possible pairs.
Input: 2, 3
Output: -1
Explanation: Clearly there doesn’t exits any pair for which gcd is greater than 1, Hence we simply return -1.Input: 2 10
Output: 2 4
Explanation: The smallest pair for which GCD is greater than 1 is 2, 4 .
Approach: The problem can be based on the following observation:
Two consecutive even numbers have GCD greater than 1 and the difference among them is also minimum.
Based on the above observation, it is optimal to find the pair of two consecutive even numbers whose values are closest to the lower limit of the range. Follow the step below to solve this problem:
- Check the absolute difference of the given range abs(A – B)
- If abs(A – B) <= 1, then there will not exists any pair, whose gcd is greater than 1, print -1.
- If the first value is even
- print (A, A+2) because these are the smallest numbers whose GCD is greater than 1.
- If the first value is odd
- Check if (B – A) == 2 then print -1 because the GCD of two consecutive odd numbers is always 1
- Check if (B – A) > 2 then
- If A is divisible by 3 then print (A, A+3),
- Otherwise print (A+1, A +3).
Below is the implementation of the above approach:
C++
// C++ code to implement the above approach#include <bits/stdc++.h>using namespace std;// Function to find the minimum Sum pair// such that their GCD(x, y) > 1void minPair(int A, int B){ // If absoulute difference is 1 // then no such pair exists if (abs(B - A) <= 1) cout << "-1"; else { // Check if first number is even if ((A % 2) == 0) cout << A << " " << A + 2; else { if (B - A == 2) { cout << -1; } else { // Check if first number is // divisible by 3 if (A % 3 == 0) { cout << A << " " << A + 3; } else { cout << A + 1 << " " << A + 3; } } } }}// Drive codeint main(){ int A = 2; int B = 10; // Function call minPair(A, B); return 0;} |
Java
// Java code to implement the above approachimport java.io.*;class GFG{ // Function to find the minimum Sum pair // such that their GCD(x, y) > 1 public static void minPair(int A, int B) { // If absoulute difference is 1 // then no such pair exists if (Math.abs(B - A) <= 1) System.out.print("-1"); else { // Check if first number is even if ((A % 2) == 0) System.out.print(A + " " + (A + 2)); else { if (B - A == 2) { System.out.print("-1"); } else { // Check if first number is // divisible by 3 if (A % 3 == 0) { System.out.print(A + " " + (A + 3)); } else { System.out.print((A + 1) + " " + (A + 3)); } } } } } // Driver Code public static void main(String[] args) { int A = 2; int B = 10; // Function call minPair(A, B); }}// This code is contributed by Rohit Pradhan |
Python3
# Python3 code to implement the above approach# Function to find the minimum Sum pair# such that their GCD(x, y) > 1def minPair(A, B): # If absoulute difference is 1 # then no such pair exists if (abs(B - A) <= 1): print("-1") else: # Check if first number is even if ((A % 2) == 0): print(f"{A} {A + 2}") else: if (B - A == 2): print(-1) else: # Check if first number is # divisible by 3 if (A % 3 == 0): print(f"{A} {A + 3}") else: print(f"{A + 1} {A + 3}")# Drive codeif __name__ == "__main__": A = 2 B = 10 # Function call minPair(A, B) # This code is contributed by rakeshsahni |
C#
// C# to implement the above approachusing System;class GFG{ // Function to find the minimum Sum pair // such that their GCD(x, y) > 1 static void minPair(int A, int B) { int f = (B-A); if(f<0) { f = f*(-1);} // If absoulute difference is 1 // then no such pair exists if (f <= 1) Console.WriteLine(-1); else { // Check if first number is even int z = A+2; if ((A % 2) == 0) Console.Write(A + " " + z); else { if (B - A == 2) { Console.Write(-1); } else { int y = A+3; // Check if first number is // divisible by 3 if (A % 3 == 0) { Console.Write(A + " " + y); } else { int c = A+1; int d = A+3; Console.Write(c + " " + d); } } } } } // Drive code public static void Main() { int A = 2; int B = 10; // Function call minPair(A, B); }}// This code is contributed by ashishsingh13122000. |
Javascript
<script> // JavaScript program for the above approach // Function to find the minimum Sum pair // such that their GCD(x, y) > 1 function minPair(A, B) { // If absoulute difference is 1 // then no such pair exists if (Math.abs(B - A) <= 1) document.write("-1"); else { // Check if first number is even if ((A % 2) == 0) document.write(A + " " + (A + 2)); else { if (B - A == 2) { document.write(-1); } else { // Check if first number is // divisible by 3 if (A % 3 == 0) { document.write(A + " " + (A + 3)); } else { document.write((A + 1) + " " + (A + 3)); } } } } } // Drive code let A = 2; let B = 10; // Function call minPair(A, B); // This code is contributed by Potta Lokesh </script> |
2 4
Time Complexity: O(1)
Auxiliary Space: O(1)


