Welcome to Subscribe On Youtube

4010. Maximize Pair Strength Using GCD

Description

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])2.

Return the maximum strength over all possible pairs.

 

Example 1:

Input: nums = [2,3,5]

Output: 15

Explanation:

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

Example 2:

Input: nums = [4,6,8]

Output: 12

Explanation:

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

Example 3:

Input: nums = [3,3]

Output: 1

Explanation:

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

 

Constraints:

  • 2 <= nums.length <= 2000
  • 1 <= nums[i] <= 105

Solutions

Solution 1: Enumeration

Thinking

Pair strength depends only on the two values and their $\gcd$. For $n\le 2000$ there are about $2\times 10^6$ unordered pairs.

We enumerate $i<j$, compute $\frac{\textit{nums}[i]\cdot\textit{nums}[j]}{\gcd^2}$ by Euclidean algorithm, and keep the maximum.

$O(n^2\log M)$ already fits the limits, so grouping the array by common divisors is unnecessary.

We directly enumerate all pairs $(i, j)$ where $i < j$, calculate the strength of each pair $\frac{\textit{nums}[i] \times \textit{nums}[j]}{\gcd(\textit{nums}[i], \textit{nums}[j])^2}$, and take the maximum.

The greatest common divisor $\gcd$ can be computed using the Euclidean algorithm.

The time complexity is $O(n^2 \times \log M)$, where $n$ is the length of the array $\textit{nums}$ and $M$ is the maximum value in the array. The space complexity is $O(1)$.

  • class Solution {
        public long maxPairStrength(int[] nums) {
            int n = nums.length;
            long ans = 0;
    
            for (int i = 0; i < n; i++) {
                for (int j = i + 1; j < n; j++) {
                    long g = gcd(nums[i], nums[j]);
                    long x = (long) nums[i] * nums[j] / (g * g);
                    ans = Math.max(ans, x);
                }
            }
    
            return ans;
        }
    
        private long gcd(long a, long b) {
            while (b != 0) {
                long t = a % b;
                a = b;
                b = t;
            }
            return a;
        }
    }
    
  • class Solution {
    public:
        long long maxPairStrength(vector<int>& nums) {
            int n = nums.size();
            long long ans = 0;
    
            for (int i = 0; i < n; i++) {
                for (int j = i + 1; j < n; j++) {
                    long long g = gcd(nums[i], nums[j]);
                    long long x = 1LL * nums[i] * nums[j] / (g * g);
                    ans = max(ans, x);
                }
            }
    
            return ans;
        }
    };
    
  • class Solution:
        def maxPairStrength(self, nums: list[int]) -> int:
            n = len(nums)
            ans = 0
            for i in range(n):
                for j in range(i + 1, n):
                    x = nums[i] * nums[j] // gcd(nums[i], nums[j]) ** 2
                    ans = max(ans, x)
            return ans
    
    
  • func maxPairStrength(nums []int) int64 {
    	n := len(nums)
    	var ans int64 = 0
    
    	for i := 0; i < n; i++ {
    		for j := i + 1; j < n; j++ {
    			g := gcd(int64(nums[i]), int64(nums[j]))
    			x := int64(nums[i]) * int64(nums[j]) / (g * g)
    			ans = max(ans, x)
    		}
    	}
    
    	return ans
    }
    
    func gcd(a, b int64) int64 {
    	for b != 0 {
    		a, b = b, a%b
    	}
    	return a
    }
    
    
  • function maxPairStrength(nums: number[]): number {
        const n = nums.length;
        let ans = 0;
    
        for (let i = 0; i < n; i++) {
            for (let j = i + 1; j < n; j++) {
                const g = gcd(nums[i], nums[j]);
                const x = Math.floor((nums[i] * nums[j]) / (g * g));
                ans = Math.max(ans, x);
            }
        }
    
        return ans;
    }
    
    function gcd(a: number, b: number): number {
        while (b !== 0) {
            const t = a % b;
            a = b;
            b = t;
        }
        return a;
    }
    
    

All Problems

All Solutions