In this blog post, we will discuss an intriguing problem from LeetCode Weekly Contest 431: "Maximum Subarray With Equal Products." We will break down the problem statement, understand the logic behind the solution, and explore a working implementation in C++.
Problem Statement
You are given an array of positive integers nums.
An array arr is called product equivalent if:
Where:
- prod(arr): The product of all elements in
arr. - gcd(arr): The greatest common divisor of all elements in
arr. - lcm(arr): The least common multiple of all elements in
arr.
Your task is to return the length of the longest product equivalent subarray in nums.
A subarray is defined as a contiguous non-empty sequence of elements within an array.
Examples
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].
For this subarray:
- Product =
1 * 2 * 1 * 1 * 1 = 2 - GCD =
1 - LCM =
2Sinceprod = gcd * lcm, the subarray is valid, and its length is5.
Example 2:
Input: nums = [2, 3, 4, 5, 6]
Output: 3
Explanation:
The longest product equivalent subarray is [3, 4, 5].
For this subarray:
- Product =
3 * 4 * 5 = 60 - GCD =
1 - LCM =
60Sinceprod = gcd * lcm, the subarray is valid, and its length is3.
Example 3:
Input: nums = [1, 2, 3, 1, 4, 5, 1]
Output: 5
Explanation:
The longest product equivalent subarray is [2, 3, 1, 4, 5].
For this subarray:
- Product =
120 - GCD =
1 - LCM =
120Sinceprod = gcd * lcm, the subarray is valid, and its length is5.
Constraints
Solution Approach
This problem involves three mathematical concepts:
- Product (prod): Multiplying all elements in a subarray.
- Greatest Common Divisor (gcd): The largest integer that divides all elements of a subarray.
- Least Common Multiple (lcm): The smallest integer that is a multiple of all elements in a subarray.
The condition prod(arr) == lcm(arr) * gcd(arr) simplifies into a computational problem where we need to calculate the GCD and LCM iteratively for each subarray while avoiding overflow issues.
Steps to Solve
Iterate Through All Subarrays:
- Start with each index as the starting point of a subarray.
- Extend the subarray to index and calculate the product, GCD, and LCM dynamically.
Check the Condition:
- For each subarray, check if . If true, update the maximum subarray length.
Prevent Overflow:
- Since the product of the elements can grow exponentially, ensure the product does not exceed the limits of a 64-bit integer.
Optimize Calculations:
- Use the formula to calculate LCM efficiently.
C++ Code Implementation
Here’s the complete C++ code for solving the problem:
Key Points
GCD and LCM Calculation:
- GCD is calculated using the built-in
gcdfunction in C++. - LCM is calculated using the formula:
- GCD is calculated using the built-in
Overflow Prevention:
- If the product exceeds the maximum limit of a 64-bit integer (
LLONG_MAX), break the loop.
- If the product exceeds the maximum limit of a 64-bit integer (
Time Complexity:
- The algorithm runs in , where is the length of the array. For each subarray, GCD and LCM are calculated efficiently.
Space Complexity:
- The solution uses only scalar variables, resulting in additional space.
Example Walkthrough
Input: nums = [1, 2, 1, 2, 1, 1, 1]
- Subarray
[1, 2, 1, 1, 1]satisfies the condition. - Length =
5.
Input: nums = [2, 3, 4, 5, 6]
- Subarray
[3, 4, 5]satisfies the condition. - Length =
3.
Input: nums = [1, 2, 3, 1, 4, 5, 1]
- Subarray
[2, 3, 1, 4, 5]satisfies the condition. - Length =
5.