Back to November Back to Year 2026
EASY 24 Nov 2026 View on LeetCode

3411. Maximum Subarray With Equal Products

</> Solution

/**
 * Problem: LeetCode 3411 - Maximum Subarray With Equal Products
 * Difficulty: Easy
 * Language: Java
 * Author: CodeByTushu
 */
class Solution {
    public int maxLength(int[] nums) {
        int n = nums.length;
        int ans = 0;

        for (int i = 0; i < n; i++) {
            long prod = 1;
            long g = 0;
            long l = 1;

            for (int j = i; j < n; j++) {
                prod *= nums[j];
                g = gcd(g, nums[j]);
                l = lcm(l, nums[j]);

                if (prod == l * g) {
                    ans = Math.max(ans, j - i + 1);
                }
            }
        }

        return ans;
    }

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

    private long lcm(long a, long b) {
        return a / gcd(a, b) * b;
    }
}

TIME COMPLEXITY

O(n²)

SPACE COMPLEXITY

O(1)

TOPICS

Array Math Number Theory Sliding Window Subsequence