We are given an array of positive integers. Find the pair in array with maximum GCD.

**Examples:**

Input : arr[] : { 1 2 3 4 5 } Output : 2 Explanation : Pair {2, 4} has GCD 2 which is highest. Other pairs have a GCD of 1. Input : arr[] : { 2 3 4 8 8 11 12 } Output : 8 Explanation : Pair {8, 8} has GCD 8 which is highest.

**Method 1 (Brute-force)**: The simplest method to solve this problem is to use two loops to generate all possible pairs of elements of the array and calculate and compare the GCD at the same time. We can use the Extended Euclidean algorithm for efficiently computing GCD of two numbers.

**Time Complexity**: O(N^2 * log(max(a, b)))

Here, log(max(a, b)) is the time complexity to calculate GCD of a and b.

**Method 2 : (Efficient)** In this method, we maintain a count array to store the count of divisors of every element. We will traverse the given array and for every element we will calculate its divisors and increment at the index of count array. The process of computing divisors will take O(sqrt(arr[i])) time, where arr[i] is element in the given array at index i. After the whole traversal, we can simply traverse the count array from last index to index 1. If we found an index with value greater than 1, then this means that it is divisor of 2 elements and also the the max GCD.

Below is the implementation of above approach :

## C++

#include <bits/stdc++.h> using namespace std; // function to find GCD of pair with // max GCD in the array int findMaxGCD(int arr[], int n) { // Computing highest element int high = 0; for (int i = 0; i < n; i++) high = max(high, arr[i]); // Array to store the count of divisors // i.e. Potential GCDs int divisors[high + 1] = { 0 }; // Iterating over every element for (int i = 0; i < n; i++) { // Calculating all the divisors for (int j = 1; j <= sqrt(arr[i]); j++) { // Divisor found if (arr[i] % j == 0) { // Incrementing count for divisor divisors[j]++; // Element/divisor is also a divisor // Checking if both divisors are // not same if (j != arr[i] / j) divisors[arr[i] / j]++; } } } // Checking the highest potential GCD for (int i = high; i >= 1; i--) // If this divisor can divide at least 2 // numbers, it is a GCD of at least 1 pair if (divisors[i] > 1) return i; } // Driver code int main() { // Array in which pair with max GCD // is to be found int arr[] = { 1, 2, 4, 8, 8, 12 }; // Size of array int n = sizeof(arr) / sizeof(arr[0]); cout << findMaxGCD(arr,n); return 0; }

## Java

// JAVA Code for Find pair with maximum GCD in an array class GFG { // function to find GCD of pair with // max GCD in the array public static int findMaxGCD(int arr[], int n) { // Computing highest element int high = 0; for (int i = 0; i < n; i++) high = Math.max(high, arr[i]); // Array to store the count of divisors // i.e. Potential GCDs int divisors[] =new int[high + 1]; // Iterating over every element for (int i = 0; i < n; i++) { // Calculating all the divisors for (int j = 1; j <= Math.sqrt(arr[i]); j++) { // Divisor found if (arr[i] % j == 0) { // Incrementing count for divisor divisors[j]++; // Element/divisor is also a divisor // Checking if both divisors are // not same if (j != arr[i] / j) divisors[arr[i] / j]++; } } } // Checking the highest potential GCD for (int i = high; i >= 1; i--) // If this divisor can divide at least 2 // numbers, it is a GCD of at least 1 pair if (divisors[i] > 1) return i; return 1; } /* Driver program to test above function */ public static void main(String[] args) { // Array in which pair with max GCD // is to be found int arr[] = { 1, 2, 4, 8, 8, 12 }; // Size of array int n = arr.length; System.out.println(findMaxGCD(arr,n)); } } // This code is contributed by Arnav Kr. Mandal.

## Python

# Python program to Find pair with # maximum GCD in an array import math # function to find GCD of pair with # max GCD in the array def findMaxGCD(arr, n) : # Computing highest element high = 0 i = 0 while i < n : high = max(high, arr[i]) i = i + 1 # Array to store the count of divisors # i.e. Potential GCDs divisors = [0] * (high + 1) # Iterating over every element i = 0 while i < n : # Calculating all the divisors j = 1 while j <= math.sqrt(arr[i]) : # Divisor found if (arr[i] % j == 0) : # Incrementing count for divisor divisors[j]= divisors[j]+1 # Element/divisor is also a divisor # Checking if both divisors are # not same if (j != arr[i] / j) : divisors[arr[i] / j] = divisors[arr[i] / j] + 1 j = j + 1 i = i + 1 # Checking the highest potential GCD i = high while i >= 1 : # If this divisor can divide at least 2 # numbers, it is a GCD of at least 1 pair if (divisors[i] > 1) : return i i = i - 1 return 1 # Driver code # Array in which pair with max GCD # is to be found arr = [ 1, 2, 4, 8, 8, 12 ] # Size of array n = len(arr) print findMaxGCD(arr,n) # This code is contributed by Nikita Tiwari.

Output:

8

**Time Complexity**: O(N * sqrt(arr[i])) , where arr[i] denotes the element of the array.

**Method 3 (Most Efficient)**: This approach is based on the idea of Sieve Of Eratosthenes.

First let’s solve a simpler problem, given a value X we have to tell whether a pair has a GCD equal to X. This can be done by checking that how many elements in the array are multiples of X. If the number of such multiples is greater than 1, then X will be a GCD of some pair.

Now for pair with maximum GCD, we maintain a count array of the original array. Our method is based on the above problem with Sieve-like approach for loop. Below is the step by step algorithm of this approach:

- Iterate ‘i’ from MAX (maximum array element) to 1.
- Iterate ‘j’ from ‘i’ to MAX. We will check if the count array is 1 at index ‘j’.
- Increment the index ‘j’ everytime with ‘i’. This way, we can check for

i, 2i, 3i, and so on. - If we get 1 two times at count array that means
**2 multiples of i**exists.This makes it the highest GCD.

Below is the implementation of above approach :

## C++

// C++ Code to // Find pair with // maximum GCD in // an array #include <bits/stdc++.h> using namespace std; // function to find // GCD of pair with // max GCD in the // array int findMaxGCD(int arr[], int n) { // Calculating MAX in array int high = 0; for (int i = 0; i < n; i++) high = max(high, arr[i]); // Maintaining count array int count[high + 1]; for (int i = 0; i < n; i++) count[arr[i]] = 1; // Variable to store the // multiples of a number int counter = 0; // Iterating from MAX to 1 // GCD is always between // MAX and 1. The first // GCD found will be the // highest as we are // decrementing the potential // GCD for (int i = high; i >= 1; i--) { int j = i; // Iterating from current // potential GCD // till it is less than // MAX while (j <= high) { // A multiple found if (count[j] == 1) counter++; // Incrementing potential // GCD by itself // To check i, 2i, 3i.... j += i; // 2 multiples found, // max GCD found if (counter == 2) return i; } } } // Driver code int main() { // Array in which pair // with max GCD is to // be found int arr[] = { 1, 2, 4, 8, 8, 12 }; // Size of array int n = sizeof(arr) / sizeof(arr[0]); cout << findMaxGCD(arr, n); return 0; }

## Java

// Java Code to // Find pair with // maximum GCD in // an array class GFG { // function to find // GCD of pair with // max GCD in the // array public static int findMaxGCD(int arr[], int n) { // Calculating MAX in // array int high = 0; for (int i = 0; i < n; i++) high = Math.max(high, arr[i]); // Maintaining count array int count[]=new int[high + 1]; for (int i = 0; i < n; i++) count[arr[i]] = 1; // Variable to store // the multiples of // a number int counter = 0; // Iterating from MAX // to 1 GCD is always // between MAX and 1 // The first GCD found // will be the highest // as we are decrementing // the potential GCD for (int i = high; i >= 1; i--) { int j = i; // Iterating from current // potential GCD till it // is less than MAX while (j <= high) { // A multiple found if (count[j] == 1) counter++; // Incrementing potential // GCD by itself // To check i, 2i, 3i.... j += i; // 2 multiples found, // max GCD found if (counter == 2) return i; } } return 1; } /* Driver program to test above function */ public static void main(String[] args) { // Array in which pair // with max GCD is to // be found int arr[] = { 1, 2, 4, 8, 8, 12 }; // Size of array int n = arr.length; System.out.println(findMaxGCD(arr,n)); } } // This code is contributed by Arnav Kr. Mandal.

## Python3

# Python3 Code to # Find pair with # maximum GCD in # an array # function to find # GCD of pair with # max GCD in the # array def findMaxGCD(arr, n) : # Calculating MAX in # array high = 0 for i in range(0, n) : high = max(high, arr[i]) # Maintaining count array count = [0] * (high + 1) for i in range(0, n) : count[arr[i]] = 1 # Variable to store the # multiples of a number counter = 0 # Iterating from MAX # to 1 GCD is always # between MAX and 1 # The first GCD found # will be the highest # as we are decrementing # the potential GCD for i in range(high, 0, -1) : j = i # Iterating from current # potential GCD till it # is less than MAX while (j <= high) : # A multiple found if (count[j] == 1) : counter = counter + 1 # Incrementing potential # GCD by itself # To check i, 2i, 3i.... j = j + i # 2 multiples found, # max GCD found if (counter == 2) : return i # Driver code # Array in which pair # with max GCD is to # be found arr = [ 1, 2, 4, 8, 8, 12 ] # Size of array n = len(arr) print(findMaxGCD(arr, n)) #This code is contributed by Nikita Tiwari.

Output:

8

**Time Complexity**: The time complexity of this approach is till an open problem known as the Dirichlet divisor problem.

This article is contributed by **Rohit Thapliyal**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.