AlgoLounge

Maximize Pair Strength Using GCD

easy

You are given an integer array nums.

Choose exactly one pair of distinct indices i and j. The strength of the pair is defined as (nums[i] * nums[j]) / gcd(nums[i], nums[j])².

Return the maximum strength over all possible pairs.

Examples

Example 1

Input:  nums = [2,3,5]
Output: 15

Choosing i = 1 and j = 2 gives strength (3 * 5) / gcd(3, 5)² = 15 / 1 = 15, which is the maximum over all pairs.

Example 2

Input:  nums = [4,6,8]
Output: 12

Choosing i = 1 and j = 2 gives strength (6 * 8) / gcd(6, 8)² = 48 / 4 = 12, which is the maximum over all pairs.

Example 3

Input:  nums = [3,3]
Output: 1

Choosing i = 0 and j = 1 gives strength (3 * 3) / gcd(3, 3)² = 9 / 9 = 1, the maximum over all pairs.

Constraints

  • 2 <= nums.length <= 2000
  • 1 <= nums[i] <= 10⁵
TypeScript
Loading editor…
List of test cases (10)
Case 1
nums = [2,3,5]
Case 2
nums = [4,6,8]
Case 3
nums = [3,3]
Case 4
nums = [1,1]
Case 5
nums = [1,100000]
Case 6
nums = [6,10,15]
Case 7
nums = [12,18,25]
Case 8
nums = [8,12,16,18]
Case 9
nums = [100000,99999]
Case 10
nums = [99991,99991,2]