Talentd
FleetCode

3411. Maximum Subarray With Equal Products

Easy47.2% acceptance1 company

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

Asked at 1 company

Hints open one at a time

Hint 1
What is the maximum possible lcm?
13:46
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def maxLength(self, nums: List[int]) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
[1,2,1,2,1,1,1]
Input
[2,3,4,5,6]
Input
[1,2,3,1,4,5,1]
Your code is saved on this device.Solution