Skip to Content
LeetCode The Hard Way
3400 - 3499

3411 - Maximum Subarray With Equal Products (Easy)

https://leetcode.com/problems/maximum-subarray-with-equal-products

Problem Statement

You are given an array of positive integers nums.

An array arr is called product equivalent if prod(arr) == lcm(arr) * gcd(arr), where:

  • prod(arr) is the product of all elements of arr.
  • gcd(arr) is the GCD of all elements of arr.
  • lcm(arr) is the LCM of all elements of arr.

Return the length of the longest product equivalent subarray of nums.

Example 1:

Input: nums = [1,2,1,2,1,1,1]

Output: 5

Explanation:

The longest product equivalent subarray is [1, 2, 1, 1, 1], where prod([1, 2, 1, 1, 1]) = 2, gcd([1, 2, 1, 1, 1]) = 1, and lcm([1, 2, 1, 1, 1]) = 2.

Example 2:

Input: nums = [2,3,4,5,6]

Output: 3

Explanation:

The longest product equivalent subarray is [3, 4, 5].

Example 3:

Input: nums = [1,2,3,1,4,5,1]

Output: 5

Constraints:

  • 2 <= nums.length <= 100
  • 1 <= nums[i] <= 10

Approach 1: Sliding Window

We use a sliding window to find the longest valid subarray. Every subarray of length 22 is always product equivalent because, for any two positive integers aa and bb: a×b=gcd(a,b)×lcm(a,b)a × b = gcd(a, b) × lcm(a, b). Therefore, we initialize res=2res = 2.

For subarrays with at least three elements, the condition is satisfied when no prime factor appears in two different elements. For example, [2,3,5][2, 3, 5] is valid because the numbers use different prime factors. However, [2,3,6][2, 3, 6] is invalid because 66 shares prime factor 22 with 22 and prime factor 33 with 33.

Since every number is at most 1010, the only possible prime factors are 22, 33, 55, and 77. We represent them using four bits:

  • Bit 00 represents 22
  • Bit 11 represents 33
  • Bit 22 represents 55
  • Bit 33 represents 77

The array mm stores the prime-factor bitmask of every number from 00 to 1010. For example:

  • m[2]=1m[2] = 1 because 22 has prime factor 22
  • m[3]=2m[3] = 2 because 33 has prime factor 33
  • m[6]=3m[6] = 3 because 66 has prime factors 22 and 33
  • m[10]=5m[10] = 5 because 1010 has prime factors 22 and 55
  • m[1]=0m[1] = 0 because 11 has no prime factors

The variable uu stores all prime factors currently used inside the window. For each new number xx, u&m[x]u \& m[x] checks whether xx shares a prime factor with any number already in the window. If there is a shared prime factor, we repeatedly remove numbers from the left side of the window. We continue until the new number has no conflict with the remaining elements.

After that, we add the new number's prime factors to uu and update resres using the current window length. The number 11 never causes a conflict because its mask is 00. Therefore, any number of 11s can remain in the window.

The time complexity is O(n)O(n) and the space complexity is O(1)O(1).

Written by@wkw
class Solution:
    def maxLength(self, nums: List[int]) -> int:
        m = [0, 0, 1, 2, 1, 4, 3, 8, 1, 2, 5]
        l = u = 0
        res = 2
        for r, x in enumerate(nums):
            while u & m[x]:
                u ^= m[nums[l]]
                l += 1
            u |= m[x]
            res = max(res, r - l + 1)
        return res

On this page