VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-pair-maximum-gcd-array/

⇱ Max GCD Pair in an Array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Max GCD Pair in an Array

Last Updated : 30 May, 2026

Given an array arr[] of size n, containing positive integers, find the maximum possible GCD among all pairs of elements in the array.

Examples:

Input: arr[] = [1, 2, 3, 4, 5]
Output: 2
Explanation: The pair (2, 4) has GCD 2, which is the maximum among all pairs.

Input: arr[] = [2, 4, 8, 12]
Output: 4
Explanation: The pair (4, 8) or (4, 12) has GCD 4, which is the maximum possible among all pairs in the array.

[Naive Approach] Check Every Pair - O(n² * log(maxElement)) Time and O(1) Space

The simplest idea is to check the GCD of every possible pair in the array and keep track of the maximum GCD obtained.


Output
4

[Better Approach] Divisor Counting - O(n * sqrt(maxElement) ) Time and O(maxElement) Space

The idea is based on a simple observation that a number can be the GCD of a pair only if it divides them. So instead of checking all pairs, we focus on divisors of each element. For every number in the array, we find all its divisors and maintain a count array count[i], where count[i] stores how many array elements are divisible by i.

  • Find the maximum element in the array.
  • Create a count array count[] of size maxElement + 1, initialized with 0.
  • Traverse array and for every element, find all its divisors from 1 to sqrt(element).
  • If i divides the element, increase count[i] amd increase count[element / i] if it is different from i.
  • After processing all elements, traverse the count[] array from largest to smallest.
  • The first index i such that count[i] > 1 is the required answer.

Consider array: arr[] = [2, 4, 8, 12]

Maximum element: 12 -> create count[0...12] = 0.

Step 1: Find divisors and update count

  • 2 -> 1, 2
  • 4 -> 1, 2, 4
  • 8 -> 1, 2, 4, 8
  • 12 -> 1, 2, 3, 4, 6, 12

Step 2: Updated count values

  • count[1] = 4
  • count[2] = 4
  • count[4] = 3
  • count[8] = 1
  • count[12] = 1

Step 3: Final count array

Index :1 2 3 4 5 6 7 8 9 10 11 12
Count :4 4 1 3 0 1 0 1 0 0 0 1

Step 4: Traverse from largest to smallest

  • 12 -> count = 1
  • 11 -> count = 0
  • ...
  • 4 -> count = 3

Since count[4] > 1, 4 divides at least two numbers. So, the maximum possible GCD is: 4 


Output
4

[Expected Approach] Sieve-based Counting - O( (maxElement) * log (maxElement) ) Time and O(maxElement) Space

The approach is based on a key observation: if a number g is the GCD of a pair, then both numbers must be divisible by g. So, instead of checking all pairs, we focus on possible GCD values. We first build a frequency array freq[], where freq[i] stores the count of occurrences of i in the array.

Now, we iterate over all possible values of g from the largest element down to 1 and check all multiples of g (i.e., g, 2g, 3g, ...) using the frequency array. If the total count of numbers divisible by g is at least 2, then g can be the GCD of some pair. Since we process values in decreasing order, the first valid g encountered is the maximum possible GCD.

  • Find the maximum element in the array (maxElement).
  • Create a frequency array freq[] of size maxElement + 1.
  • Store frequency of each element in the array.
  • Iterate g from maxElement down to 1.
  • For each g, traverse all multiples of g.
  • Keep adding freq[multiple] to a count.
  • If count ≥ 2, return g as the answer.

Consider array: arr = [2, 4, 8, 12]

Step 1: Build Frequency Array

  • freq[2] = 1
  • freq[4] = 1
  • freq[8] = 1
  • freq[12] = 1

(All other values have frequency 0)

Step 2: Check from Largest to Smallest

  • g = 12 -> only 12 -> count = 1
  • g = 11 -> no multiples -> count = 0
  • g = 10 -> no multiples -> count = 0
  • g = 9 -> no multiples -> count = 0
  • g = 8 -> only 8 -> count = 1
  • g = 7 -> no multiples -> count = 0
  • g = 6 -> only 12 -> count = 1
  • g = 5 -> no multiples -> count = 0
  • g = 4 -> 4, 8, 12 -> count = 3

Since g = 4 is the first value from the top having count ≥ 2, so Maximum GCD = 4


Output
4
Comment
Article Tags:
Article Tags: