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:1Explanation:

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++

`// 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;` `}` |

## Java

`// 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` |

## Python3

`# 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` |

## C#

`// 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` |

**Output:**

1

**Time Complexity:** *O(N)***Auxiliary Space: ***O(1)*

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price.

In case you wish to attend live classes with industry experts, please refer **Geeks Classes Live** and **Geeks Classes Live USA**