Code Room
CodingMediumcod-g837
Subject Gcd lcmLevel Mid–Senior~25 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

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.

Implement
max_pair_gcd(arr: list[int]) → int
Examples
in[[12,18,24]]out12
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.