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++
// 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) > 1
void minPair(int A, int B)
{
// If absolute 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 code
int main()
{
int A = 2;
int B = 10;
// Function call
minPair(A, B);
return 0;
}
C
// C code to implement the above approach
#include <stdio.h>
// Function to find the minimum Sum pair
// such that their GCD(x, y) > 1
void minPair(int A, int B)
{
int check = (B-A);
if(check<0){ check = check*(-1); }
// If absolute difference is 1
// then no such pair exists
if (check <= 1)
printf("-1");
else {
// Check if first number is even
if ((A % 2) == 0)
printf("%d %d", A, A + 2);
else {
if (B - A == 2) {
printf("-1");
}
else {
// Check if first number is
// divisible by 3
if (A % 3 == 0) {
printf("%d %d",A, A + 3);
}
else {
printf("%d %d",A + 1,
A + 3);
}
}
}
}
}
// Drive code
void main()
{
int A = 2;
int B = 10;
// Function call
minPair(A, B);
}
// This code is contributed by ashishsingh13122000.
Java
// Java code to implement the above approach
import 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 absolute 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) > 1
def minPair(A, B):
# If absolute 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 code
if __name__ == "__main__":
A = 2
B = 10
# Function call
minPair(A, B)
# This code is contributed by rakeshsahni
C#
// C# to implement the above approach
using 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 absolute 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 absolute 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>
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Find two numbers whose sum and GCD are given Given sum and gcd of two numbers a and b . The task is to find both the numbers a and b. If the numbers do not exist then print -1 .Examples: Input: sum = 6, gcd = 2 Output: a = 4, b = 2 4 + 2 = 6 and GCD(4, 2) = 2Input: sum = 7, gcd = 2 Output: -1 There are no such numbers whose sum is 7 and GCD is
5 min read
Find N - 1 pairs from given array such that GCD of all pair-sums is greater than 1 Given an array arr[] of 2 * N integers, the task is to find a set of N - 1 pairs such that the GCD of all pair sums is greater than 1. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6}Output:1 32 4Explanation: The given array has 3 * 2 elements. The pair (1, 3) and (2, 4) has sum of elements as 4 and 6 re
7 min read
Find pair with maximum GCD for integers in range 2 to N Given a number N, the task is to find a pair of integers in the range [2, N] with maximum GCD.Examples: Input: N = 10 Output: 5 Explanation: Maximum possible GCD between all possible pairs is 5 which occurs for the pair (10, 5).Input: N = 13 Output: 6 Explanation: Maximum possible GCD between all po
8 min read
Find the number of pairs such that their gcd is equals to 1 Given an array a of size N. The task is to find the number of pairs such that gcd(a[i], a[j]) is equal to 1, where 1 \leq i < j \leq N. Examples: Input : a[] = {1, 2, 4, 6} Output : 3 {1, 2}, {1, 4}, {1, 6} are such pairsInput : a[] = {1, 2, 3, 4, 5, 6} Output : 11 Approach : The answer is to sum
10 min read
Find the maximum GCD possible for some pair in a given range [L, R] Given a range L to R, the task is to find the maximum possible value of GCD(X, Y) such that X and Y belongs to the given range, i.e. L ? X < Y ? R. Examples: Input: L = 101, R = 139Output:34Explanation:For X = 102 and Y = 136, the GCD of x and y is 34, which is the maximum possible. Input: L = 8,
5 min read
Find N distinct integers with GCD of sequence as 1 and GCD of each pair greater than 1 Given an integer N, the task is to find a sequence of N distinct positive integers such that the Greatest Common Divisor of the sequence is 1 and GCD of all possible pairs of elements is greater than 1. Input: N = 4Output: 84 60 105 70Explanation: The GCD(84, 60, 105, 70) is 1 and the GCD of all pos
5 min read