Maximum pair GCD

Write codeMedium25 min

Your task

Given an array of positive integers with values bounded by M (e.g. M <= 10^5), return the maximum gcd over all unordered pairs of distinct elements. The O(n^2) all-pairs gcd is too slow for large arrays; instead, for each candidate g from M down to 1, check whether at least two array elements are multiples of g using a count of values. The first g for which two multiples exist is the answer. Return 0 if the array has fewer than two elements.

Examples

max_pair_gcd(arr: list[int]) -> int
Example 1
Inputmax_pair_gcd([12, 18, 24])
Output12