Input: nums =[2,4,8,16]Output: 64Explanation:
On removing 2, the GCD of the rest of the elements is4while the LCM is16,which gives a maximum factor score of `4 * 16 = 64`.
The factor score is the product of the GCD and LCM of the array. To maximize the score after removing at most one element, we can try removing each element (including removing none) and compute the GCD and LCM for the resulting array.
classSolution {
public:int maxFactorScore(vector<int>& nums) {
int n = nums.size(), ans =0;
for(int i =-1; i < n; ++i) {
int g =0, l =1;
for(int j =0; j < n; ++j) if(j != i) {
g = gcd(g, nums[j]);
l = lcm(l, nums[j]);
}
if(n ==1&& i ==-1) ans = nums[0]*nums[0];
elseif(n ==1&& i ==0) ans =0;
else ans = max(ans, g * l);
}
return ans;
}
intgcd(int a, int b) { return b ? gcd(b, a % b) : a; }
intlcm(int a, int b) { return a / gcd(a, b) * b; }
};
funcmaxFactorScore(nums []int) int {
n, ans:= len(nums), 0gcd:=func(a, bint) int {
forb!=0 {
a, b = b, a%b }
returna }
lcm:=func(a, bint) int {
returna/gcd(a, b) *b }
fori:=-1; i < n; i++ {
g, l:=0, 1forj:=0; j < n; j++ {
ifj!=i {
g = gcd(g, nums[j])
l = lcm(l, nums[j])
}
}
ifn==1&&i==-1 {
ans = nums[0] *nums[0]
} elseifn==1&&i==0 {
ans = 0 } elseifg*l > ans {
ans = g*l }
}
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
publicintmaxFactorScore(int[] nums) {
int n = nums.length, ans = 0;
for(int i =-1; i < n; ++i) {
int g = 0, l = 1;
for(int j = 0; j < n; ++j) if(j != i) {
g = gcd(g, nums[j]);
l = lcm(l, nums[j]);
}
if(n == 1 && i ==-1) ans = nums[0]*nums[0];
elseif(n == 1 && i == 0) ans = 0;
else ans = Math.max(ans, g * l);
}
return ans;
}
intgcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
intlcm(int a, int b) { return a / gcd(a, b) * b; }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution {
funmaxFactorScore(nums: IntArray): Int {
val n = nums.size
var ans = 0fungcd(a: Int, b: Int): Int = if (b ==0) a else gcd(b, a % b)
funlcm(a: Int, b: Int): Int = a / gcd(a, b) * b
for (i in -1 until n) {
var g = 0; var l = 1for (j in0 until n) if (j != i) {
g = gcd(g, nums[j])
l = lcm(l, nums[j])
}
if (n ==1&& i == -1) ans = nums[0]*nums[0]
elseif (n ==1&& i ==0) ans = 0else ans = maxOf(ans, g * l)
}
return ans
}
}
classSolution:
defmaxFactorScore(self, nums: list[int]) -> int:
from math import gcd
deflcm(a: int, b: int) -> int:
return a * b // gcd(a, b) if a and b else a or b
n = len(nums)
ans =0for i in range(-1, n):
g, l =0, 1for j in range(n):
if j != i:
g = gcd(g, nums[j])
l = lcm(l, nums[j])
if n ==1and i ==-1:
ans = nums[0]*nums[0]
elif n ==1and i ==0:
ans =0else:
ans = max(ans, g*l)
return ans
impl Solution {
pubfnmax_factor_score(nums: Vec<i32>) -> i32 {
fngcd(mut a: i32, mut b: i32) -> i32 {
while b !=0 {
let t = b;
b = a % b;
a = t;
}
a
}
fnlcm(a: i32, b: i32) -> i32 {
if a ==0|| b ==0 { a.max(b) } else { a / gcd(a, b) * b }
}
let n = nums.len();
letmut ans =0;
for i in-1i32..n asi32 {
letmut g =0;
letmut l =1;
for j in0..n {
if j asi32!= i {
g = gcd(g, nums[j]);
l = lcm(l, nums[j]);
}
}
if n ==1&& i ==-1 {
ans = nums[0]*nums[0];
} elseif n ==1&& i ==0 {
ans =0;
} else {
ans = ans.max(g*l);
}
}
ans
}
}