Maximum Subarray With Equal Products - Problem
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 ofarrgcd(arr)is the GCD of all elements ofarrlcm(arr)is the LCM of all elements ofarr
Return the length of the longest product equivalent subarray of nums.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [1,2,3,4]
›
Output:
1
💡 Note:
Only single-element subarrays are product equivalent. For [1]: product=1, gcd=1, lcm=1, so 1 = 1×1 ✓. For [1,2]: product=2, gcd=1, lcm=2, so 2 ≠ 1×2 ✗.
Example 2 — Multiple Equal Elements
$
Input:
nums = [6,6]
›
Output:
2
💡 Note:
The subarray [6,6] is product equivalent: product=36, gcd=6, lcm=6, so 36 = 6×6 ✓. Length is 2.
Example 3 — Single Element Only
$
Input:
nums = [2,4,8]
›
Output:
1
💡 Note:
Each single element works: [2] has 2=2×1, [4] has 4=4×1, [8] has 8=8×1. But [2,4] has product=8, gcd=2, lcm=4, so 8 = 2×4 ✓, giving length 2.
Constraints
- 1 ≤ nums.length ≤ 105
- 1 ≤ nums[i] ≤ 106
Visualization
Tap to expand
Understanding the Visualization
1
Input Array
Array of positive integers
2
Check Condition
For each subarray, verify if product equals GCD × LCM
3
Find Maximum
Return length of longest valid subarray
Key Takeaway
🎯 Key Insight: Product equals GCD×LCM only when elements have special mathematical relationships, often when they're identical or share common factors
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code