A subarray is product equivalent if prod(arr) == lcm(arr) * gcd(arr). For positive integers, this holds if and only if all elements are equal or all are 1. We can use a sliding window to check for the longest such subarray.
classSolution {
public:int maxProductEquivalentSubarray(vector<int>& nums) {
int n = nums.size(), ans =1;
for (int i =0; i < n; ++i) {
longlong prod =1, g = nums[i], l = nums[i];
for (int j = i; j < n; ++j) {
prod *= nums[j];
g = gcd(g, nums[j]);
l = lcm(l, nums[j]);
if (prod == g * l) ans = max(ans, j - i +1);
elsebreak;
}
}
return ans;
}
longlonggcd(longlong a, longlong b) { return b ? gcd(b, a % b) : a; }
longlonglcm(longlong a, longlong b) { return a / gcd(a, b) * b; }
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
funcgcd(a, bint) int { forb!=0 { a, b = b, a%b }; returna }
funclcm(a, bint) int { returna/gcd(a, b) *b }
funcmaxProductEquivalentSubarray(nums []int) int {
n, ans:= len(nums), 1fori:=0; i < n; i++ {
prod, g, l:=1, nums[i], nums[i]
forj:=i; j < n; j++ {
prod*=nums[j]
g = gcd(g, nums[j])
l = lcm(l, nums[j])
ifprod==g*l {
ifj-i+1 > ans { ans = j-i+1 }
} else { break }
}
}
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
publicintmaxProductEquivalentSubarray(int[] nums) {
int n = nums.length, ans = 1;
for (int i = 0; i < n; ++i) {
long prod = 1, g = nums[i], l = nums[i];
for (int j = i; j < n; ++j) {
prod *= nums[j];
g = gcd(g, nums[j]);
l = lcm(l, nums[j]);
if (prod == g * l) ans = Math.max(ans, j - i + 1);
elsebreak;
}
}
return ans;
}
longgcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); }
longlcm(long a, long 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 {
funmaxProductEquivalentSubarray(nums: IntArray): Int {
fungcd(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
val n = nums.size
var ans = 1for (i in0 until n) {
var prod = 1L; var g = nums[i]; var l = nums[i]
for (j in i until n) {
prod *= nums[j]
g = gcd(g, nums[j])
l = lcm(l, nums[j])
if (prod == g.toLong() * l) ans = maxOf(ans, j - i + 1)
elsebreak }
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution:
defmaxProductEquivalentSubarray(self, nums: list[int]) -> int:
from math import gcd
deflcm(a, b): return a * b // gcd(a, b)
n = len(nums)
ans =1for i in range(n):
prod, g, l =1, nums[i], nums[i]
for j in range(i, n):
prod *= nums[j]
g = gcd(g, nums[j])
l = lcm(l, nums[j])
if prod == g * l:
ans = max(ans, j - i +1)
else:
breakreturn ans
impl Solution {
pubfnmax_product_equivalent_subarray(nums: Vec<i32>) -> i32 {
fngcd(mut a: i64, mut b: i64) -> i64 { while b !=0 { let t = b; b = a % b; a = t; } a }
fnlcm(a: i64, b: i64) -> i64 { a / gcd(a, b) * b }
let n = nums.len();
letmut ans =1;
for i in0..n {
let (mut prod, mut g, mut l) = (1i64, nums[i] asi64, nums[i] asi64);
for j in i..n {
prod *= nums[j] asi64;
g = gcd(g, nums[j] asi64);
l = lcm(l, nums[j] asi64);
if prod == g * l {
ans = ans.max((j - i +1) asi32);
} else { break; }
}
}
ans
}
}
⏰ Time complexity: O(n^2 log M), where n is the length of nums and M is the maximum value in nums. Each subarray is checked, and GCD/LCM are computed in log M time.
🧺 Space complexity: O(1), only a few variables are used.