3411 - Maximum Subarray With Equal Products (Easy)
Problem Link
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 ofarr.gcd(arr)is the GCD of all elements ofarr.lcm(arr)is the LCM of all elements ofarr.
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 <= 1001 <= nums[i] <= 10
Approach 1: Sliding Window
We use a sliding window to find the longest valid subarray. Every subarray of length is always product equivalent because, for any two positive integers and : . Therefore, we initialize .
For subarrays with at least three elements, the condition is satisfied when no prime factor appears in two different elements. For example, is valid because the numbers use different prime factors. However, is invalid because shares prime factor with and prime factor with .
Since every number is at most , the only possible prime factors are , , , and . We represent them using four bits:
- Bit represents
- Bit represents
- Bit represents
- Bit represents
The array stores the prime-factor bitmask of every number from to . For example:
- because has prime factor
- because has prime factor
- because has prime factors and
- because has prime factors and
- because has no prime factors
The variable stores all prime factors currently used inside the window. For each new number , checks whether 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 and update using the current window length. The number never causes a conflict because its mask is . Therefore, any number of s can remain in the window.
The time complexity is and the space complexity is .
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 res3375 - Minimum Operations to Make Array Values Equal to K
Count the distinct levels above k to determine the fewest operations needed to reduce all values to k, checking feasibility first.
3420 - Count Non-Decreasing Subarrays After K Operations
Maintain the cost of making each window non-decreasing using a monotonic deque, shrinking from the right when the cost exceeds k.