3702. Maximum Subarray With Equal Products
My accepted Python solution to LeetCode problem 3702, Maximum Subarray With Equal Products, running in 753ms.
- Difficulty: Easy
- Python
- Runtime 753ms
- Memory 17.6MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 753ms, memory 17.6MB, accepted 2026-01-01.
class Solution:
def maxLength(self, nums: List[int]) -> int:
from math import gcd, lcm
from functools import reduce
def product(arr):
return reduce(lambda x, y: x * y, arr)
def get_gcd(arr):
return reduce(gcd, arr)
def get_lcm(arr):
return reduce(lcm, arr)
n = len(nums)
max_len = 0
for i in range(n):
for j in range(i, n):
subarr = nums[i:j+1]
p = product(subarr)
g = get_gcd(subarr)
l = get_lcm(subarr)
if p == l * g:
max_len = max(max_len, j - i + 1)
return max_len