Skip to content

3334. Find the Maximum Factor Score of Array 👍

  • Time: $O(n)$
  • Space: $O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Solution {
 public:
  long long maxScore(vector<int>& nums) {
    const int n = nums.size();
    // prefixGcd[i] := GCD of nums[0..i]
    // prefixLcm[i] := LCM of nums[0..i]
    const auto [prefixGcd, prefixLcm] = getPrefix(nums);
    // suffixGcd[i] := GCD of nums[i..n - 1]
    // suffixLcm[i] := LCM of nums[i..n - 1]
    const auto [suffixGcd, suffixLcm] = getSuffix(nums);
    long ans = suffixGcd[0] * suffixLcm[0];

    for (int i = 0; i < n; ++i) {
      const long gcd1 = i > 0 ? prefixGcd[i - 1] : 0;
      const long gcd2 = i + 1 < n ? suffixGcd[i + 1] : 0;
      const long lcm1 = i > 0 ? prefixLcm[i - 1] : 1;
      const long lcm2 = i + 1 < n ? suffixLcm[i + 1] : 1;
      const long score = gcd(gcd1, gcd2) * lcm(lcm1, lcm2);
      ans = max(ans, score);
    }

    return ans;
  }

 private:
  // Returns the prefix GCD and LCM arrays.
  pair<vector<long>, vector<long>> getPrefix(const vector<int>& nums) {
    vector<long> prefixGcd;
    vector<long> prefixLcm;
    long currGcd = 0;
    long currLcm = 1;
    for (const int num : nums) {
      currGcd = gcd(currGcd, num);
      currLcm = lcm(currLcm, num);
      prefixGcd.push_back(currGcd);
      prefixLcm.push_back(currLcm);
    }
    return {prefixGcd, prefixLcm};
  }

  // Returns the suffix GCD and LCM arrays.
  pair<vector<long>, vector<long>> getSuffix(const vector<int>& nums) {
    vector<long> suffixGcd;
    vector<long> suffixLcm;
    long currGcd = 0;
    long currLcm = 1;
    for (int i = nums.size() - 1; i >= 0; --i) {
      currGcd = gcd(currGcd, nums[i]);
      currLcm = lcm(currLcm, nums[i]);
      suffixGcd.push_back(currGcd);
      suffixLcm.push_back(currLcm);
    }
    ranges::reverse(suffixGcd);
    ranges::reverse(suffixLcm);
    return {suffixGcd, suffixLcm};
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class Solution {
  public long maxScore(int[] nums) {
    final int n = nums.length;
    // prefixGcd[i] := GCD of nums[0..i]
    // prefixLcm[i] := LCM of nums[0..i]
    long[][] prefix = getPrefix(nums);
    long[] prefixGcd = prefix[0];
    long[] prefixLcm = prefix[1];
    // suffixGcd[i] := GCD of nums[i..n - 1]
    // suffixLcm[i] := LCM of nums[i..n - 1]
    long[][] suffix = getSuffix(nums);
    long[] suffixGcd = suffix[0];
    long[] suffixLcm = suffix[1];
    long ans = suffixGcd[0] * suffixLcm[0];

    for (int i = 0; i < n; ++i) {
      final long gcd1 = i > 0 ? prefixGcd[i - 1] : 0;
      final long gcd2 = i + 1 < n ? suffixGcd[i + 1] : 0;
      final long lcm1 = i > 0 ? prefixLcm[i - 1] : 1;
      final long lcm2 = i + 1 < n ? suffixLcm[i + 1] : 1;
      final long score = gcd(gcd1, gcd2) * lcm(lcm1, lcm2);
      ans = Math.max(ans, score);
    }

    return ans;
  }

  // Returns the prefix GCD and LCM arrays.
  private long[][] getPrefix(int[] nums) {
    long[] prefixGcd = new long[nums.length];
    long[] prefixLcm = new long[nums.length];
    long currGcd = 0;
    long currLcm = 1;
    for (int i = 0; i < nums.length; ++i) {
      currGcd = gcd(currGcd, nums[i]);
      currLcm = lcm(currLcm, nums[i]);
      prefixGcd[i] = currGcd;
      prefixLcm[i] = currLcm;
    }
    return new long[][] {prefixGcd, prefixLcm};
  }

  // Returns the suffix GCD and LCM arrays.
  private long[][] getSuffix(int[] nums) {
    long[] suffixGcd = new long[nums.length];
    long[] suffixLcm = new long[nums.length];
    long currGcd = 0;
    long currLcm = 1;
    for (int i = nums.length - 1; i >= 0; --i) {
      currGcd = gcd(currGcd, nums[i]);
      currLcm = lcm(currLcm, nums[i]);
      suffixGcd[i] = currGcd;
      suffixLcm[i] = currLcm;
    }
    return new long[][] {suffixGcd, suffixLcm};
  }

  private long gcd(long a, long b) {
    return b == 0 ? a : gcd(b, a % b);
  }

  private long lcm(long a, long b) {
    return a * (b / gcd(a, b));
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Solution:
  def maxScore(self, nums: list[int]) -> int:
    n = len(nums)
    # prefixGcd[i] := GCD of nums[0..i]
    # prefixLcm[i] := LCM of nums[0..i]
    prefixGcd, prefixLcm = self._getPrefix(nums)
    # suffixGcd[i] := GCD of nums[i..n - 1]
    # suffixLcm[i] := LCM of nums[i..n - 1]
    suffixGcd, suffixLcm = self._getSuffix(nums)
    ans = suffixGcd[0] * suffixLcm[0]

    for i in range(n):
      gcd1 = prefixGcd[i - 1] if i > 0 else 0
      gcd2 = suffixGcd[i + 1] if i + 1 < n else 0
      lcm1 = prefixLcm[i - 1] if i > 0 else 1
      lcm2 = suffixLcm[i + 1] if i + 1 < n else 1
      score = math.gcd(gcd1, gcd2) * math.lcm(lcm1, lcm2)
      ans = max(ans, score)

    return ans

  def _getPrefix(self, nums: list[int]) -> tuple[list[int], list[int]]:
    """Returns the prefix GCD and LCM arrays."""
    prefixGcd = []
    prefixLcm = []
    currGcd = 0
    currLcm = 1
    for num in nums:
      currGcd = math.gcd(currGcd, num)
      currLcm = math.lcm(currLcm, num)
      prefixGcd.append(currGcd)
      prefixLcm.append(currLcm)
    return prefixGcd, prefixLcm

  def _getSuffix(self, nums: list[int]) -> tuple[list[int], list[int]]:
    """Returns the suffix GCD and LCM arrays."""
    suffixGcd = []
    suffixLcm = []
    currGcd = 0
    currLcm = 1
    for num in reversed(nums):
      currGcd = math.gcd(currGcd, num)
      currLcm = math.lcm(currLcm, num)
      suffixGcd.append(currGcd)
      suffixLcm.append(currLcm)
    return list(reversed(suffixGcd)), list(reversed(suffixLcm))