Find minimum sum Pair whose GCD is greater than 1
Last Updated :
21 Nov, 2022
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++
#include <bits/stdc++.h>
using namespace std;
void minPair( int A, int B)
{
if ( abs (B - A) <= 1)
cout << "-1" ;
else {
if ((A % 2) == 0)
cout << A << " " << A + 2;
else {
if (B - A == 2) {
cout << -1;
}
else {
if (A % 3 == 0) {
cout << A << " " << A + 3;
}
else {
cout << A + 1 << " "
<< A + 3;
}
}
}
}
}
int main()
{
int A = 2;
int B = 10;
minPair(A, B);
return 0;
}
|
C
#include <stdio.h>
void minPair( int A, int B)
{
int check = (B-A);
if (check<0){ check = check*(-1); }
if (check <= 1)
printf ( "-1" );
else {
if ((A % 2) == 0)
printf ( "%d %d" , A, A + 2);
else {
if (B - A == 2) {
printf ( "-1" );
}
else {
if (A % 3 == 0) {
printf ( "%d %d" ,A, A + 3);
}
else {
printf ( "%d %d" ,A + 1,
A + 3);
}
}
}
}
}
void main()
{
int A = 2;
int B = 10;
minPair(A, B);
}
|
Java
import java.io.*;
class GFG
{
public static void minPair( int A, int B)
{
if (Math.abs(B - A) <= 1 )
System.out.print( "-1" );
else
{
if ((A % 2 ) == 0 )
System.out.print(A + " " + (A + 2 ));
else {
if (B - A == 2 ) {
System.out.print( "-1" );
}
else {
if (A % 3 == 0 ) {
System.out.print(A + " " + (A + 3 ));
}
else {
System.out.print((A + 1 ) + " "
+ (A + 3 ));
}
}
}
}
}
public static void main(String[] args)
{
int A = 2 ;
int B = 10 ;
minPair(A, B);
}
}
|
Python3
def minPair(A, B):
if ( abs (B - A) < = 1 ):
print ( "-1" )
else :
if ((A % 2 ) = = 0 ):
print (f "{A} {A + 2}" )
else :
if (B - A = = 2 ):
print ( - 1 )
else :
if (A % 3 = = 0 ):
print (f "{A} {A + 3}" )
else :
print (f "{A + 1} {A + 3}" )
if __name__ = = "__main__" :
A = 2
B = 10
minPair(A, B)
|
C#
using System;
class GFG{
static void minPair( int A, int B)
{
int f = (B-A);
if (f<0) { f = f*(-1);}
if (f <= 1)
Console.WriteLine(-1);
else
{
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;
if (A % 3 == 0) {
Console.Write(A + " " + y);
}
else {
int c = A+1;
int d = A+3;
Console.Write(c + " "
+ d);
}
}
}
}
}
public static void Main()
{
int A = 2;
int B = 10;
minPair(A, B);
}
}
|
Javascript
<script>
function minPair(A, B)
{
if (Math.abs(B - A) <= 1)
document.write( "-1" );
else
{
if ((A % 2) == 0)
document.write(A + " " + (A + 2));
else {
if (B - A == 2) {
document.write(-1);
}
else {
if (A % 3 == 0) {
document.write(A + " " + (A + 3));
}
else {
document.write((A + 1) + " "
+ (A + 3));
}
}
}
}
}
let A = 2;
let B = 10;
minPair(A, B);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...