3411. Maximum Subarray With Equal Products LeetCode Solution

In this guide, you will get 3411. Maximum Subarray With Equal Products LeetCode Solution with the best time and space complexity. The solution to Maximum Subarray With Equal Products problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Maximum Subarray With Equal Products solution in C++
  4. Maximum Subarray With Equal Products solution in Java
  5. Maximum Subarray With Equal Products solution in Python
  6. Additional Resources
3411. Maximum Subarray With Equal Products LeetCode Solution image

Problem Statement of Maximum Subarray With Equal Products

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

Complexity Analysis

  • Time Complexity: O(n^2)
  • Space Complexity: O(1)

3411. Maximum Subarray With Equal Products LeetCode Solution in C++

class Solution {
 public:
  int maxLength(const vector<int>& nums) {
    constexpr int kMaxLcm = 36'288'000;  // 10! x 10
    const int n = nums.size();
    int ans = 0;

    for (int i = 0; i < n; ++i) {
      int prod = 1;
      int l = 1;
      int g = 0;
      for (int j = i; j < n; ++j) {
        prod *= nums[j];
        if (prod > kMaxLcm)
          break;
        l = lcm(l, nums[j]);
        g = gcd(g, nums[j]);
        if (prod == l * g)
          ans = max(ans, j - i + 1);
      }
    }

    return ans;
  }
};
/* code provided by PROGIEZ */

3411. Maximum Subarray With Equal Products LeetCode Solution in Java

class Solution {
  public int maxLength(int[] nums) {
    final int kMaxLcm = 36_288_000; // 10! x 10
    final int n = nums.length;
    int ans = 0;

    for (int i = 0; i < n; ++i) {
      int prod = 1;
      int l = 1;
      int g = 0;
      for (int j = i; j < n; ++j) {
        prod *= nums[j];
        if (prod > kMaxLcm)
          break;
        l = lcm(l, nums[j]);
        g = gcd(g, nums[j]);
        if (prod == l * g)
          ans = Math.max(ans, j - i + 1);
      }
    }

    return ans;
  }

  private int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
  }

  private int lcm(int a, int b) {
    return a * (b / gcd(a, b));
  }
}
// code provided by PROGIEZ

3411. Maximum Subarray With Equal Products LeetCode Solution in Python

class Solution:
  def maxLength(self, nums: list[int]) -> int:
    n = len(nums)
    ans = 0

    for i in range(n):
      prod = 1
      l = 1
      g = 0
      for j in range(i, n):
        prod *= nums[j]
        l = math.lcm(l, nums[j])
        g = math.gcd(g, nums[j])
        if prod == l * g:
          ans = max(ans, j - i + 1)

    return ans
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.