Common Divisors

You are given an integer array nums containing positive integers. Your task is to select exactly two distinct elements from this array such that their Greatest Common Divisor (GCD) is as large as possible.

Find and return this maximum GCD value.

Example 1:

Input: nums: [3, 14, 15, 7, 9]

Output: 7

Explanation:

If we choose the numbers 14 and 7, their greatest common divisor is 7. No other pair in the array shares a larger common divisor (e.g., GCD of 15 and 9 is 3).

Example 2:

Input: nums: [2, 4, 8, 16]

Output: 8

Explanation:

If we choose the numbers 8 and 16, their greatest common divisor is exactly 8.

Now Your Turn!

Pick the correct output for the given input

Input: nums: [11, 22, 33, 44, 55]

(What is the maximum GCD between any two numbers in this array?)

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 2 <= nums.length <= 2 x105
  • 1 <= nums[i] <= 106

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
int maxGCD(vector<int>& nums) {
}
};
Test Case

Input:

Nums