Find minimum GCD of all pairs in an array


Given an array arr of positive integers, the task is to find minimum GCD possible for any pair of the given array.

Examples:

Input: arr[] = {1, 2, 3, 4, 5}
Output: 1
Explanation:
GCD(1, 2) = 1.

Input: arr[] = {2, 4, 6, 8, 3}
Output: 1

Approach:
If we observe clearly, we will notice that the minimum GCD of any two numbers will be the GCD of all the elements in the array. The reason behind this observation is that the minimum GCD for any pair occurs if the element that minimizes the GCD of the entire array is present in that pair.



Illustration:
Let us consider an array arr[] = {2,4,6,8,3}.
GCD of the all array elements excluding 3 is 2. On considering 3, the gcd minimizes to 1.
GCD of all the possible pairs are:
gcd(2,4) = 2
gcd(2,6) = 2
gcd(2,8) = 2
gcd(2,3) = 1
gcd(4,6) = 2
gcd(4,8) = 4
gcd(4,3) = 1
gcd(6,3) = 3
gcd(6,8) = 2
gcd(8,3) = 1
Thus, the minimum gcd for any pair is equal to 1, which is equal to gcd of the array and only appears for the pairs which contain 3 in it.

Below code is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the
// minimum GCD of any pair
// in the array
#include <bits/stdc++.h>
using namespace std;
  
// Function returns the
// Minimum GCD of any pair
int MinimumGCD(int arr[], int n)
{
    int g = 0;
  
    // Finding GCD of all the
    // elements in the array.
    for (int i = 0; i < n; i++) {
        g = __gcd(g, arr[i]);
    }
    return g;
}
  
// Driver code
int main()
{
    int arr[] = { 2, 4, 6, 8, 3 };
  
    int N = sizeof(arr) / sizeof(arr[0]);
  
    cout << MinimumGCD(arr, N) << endl;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the
// minimum GCD of any pair
// in the array
import java.util.*;
  
class GFG{
      
static int __gcd(int a, int b) 
{
    if(b == 0)
        return a;
    else
        return __gcd(b, a % b); 
}
  
// Function returns the
// minimum GCD of any pair
static int MinimumGCD(int arr[], int n)
{
    int g = 0;
  
    // Finding GCD of all the
    // elements in the array.
    for(int i = 0; i < n; i++)
    {
       g = __gcd(g, arr[i]);
    }
      
    return g;
}
  
// Driver code
public static void main(String[] args)
{
    int arr[] = { 2, 4, 6, 8, 3 };
    int N = arr.length;
  
    System.out.println(MinimumGCD(arr, N));
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find the
# minimum GCD of any pair
# in the array
  
from math import gcd
  
# Function returns the
# Minimum GCD of any pair
def MinimumGCD(arr, n):
    g = 0
  
    # Finding GCD of all the
    # elements in the array.
    for i in range(n):
        g = gcd(g, arr[i])
    return g
  
# Driver code
if __name__ == '__main__':
      
    arr = [ 2, 4, 6, 8, 3 ]
  
    N = len(arr)
    print(MinimumGCD(arr, N))
  
# This code is contributed by Samarth

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the
// minimum GCD of any pair
// in the array
using System;
  
class GFG{
      
static int __gcd(int a, int b) 
{
    if(b == 0)
        return a;
    else
        return __gcd(b, a % b);
}
  
// Function returns the
// minimum GCD of any pair
static int MinimumGCD(int []arr, int n)
{
    int g = 0;
  
    // Finding GCD of all the
    // elements in the array.
    for(int i = 0; i < n; i++)
    {
       g = __gcd(g, arr[i]);
    }
      
    return g;
}
  
// Driver code
public static void Main(String[] args)
{
    int []arr = { 2, 4, 6, 8, 3 };
    int N = arr.Length;
  
    Console.WriteLine(MinimumGCD(arr, N));
}
}
  
// This code is contributed by sapnasingh4991

chevron_right


Output:

1

Time Complexity: O(N)

competitive-programming-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.