Find the Maximum Factor Score of Array - Complete Solution Guide
Find the Maximum Factor Score of Array is LeetCode problem 3334, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given an integer array nums . The factor score of an array is defined as the product of the LCM and GCD of all elements of that array. Return the maximum factor score of nums after removing at most one element from it. Note that both the LCM and GCD of a single number are the number itself, and the factor score of an empty array is 0. Example 1: Input: nums = [2,4,8,16] Output: 64 Explanation: On removing 2, the GCD of the rest of the elements is 4 while the LCM is 16, which gives a maxi
Detailed Explanation
The problem asks us to find the maximum 'factor score' of an integer array `nums`. The factor score is defined as the product of the Least Common Multiple (LCM) and the Greatest Common Divisor (GCD) of all elements in the array. We are allowed to remove at most one element from the array to maximize this factor score. If the array is empty after removal, the factor score is 0. The constraints are that the array length is between 1 and 100, and each element is between 1 and 30.
Solution Approach
The solution uses a dynamic programming approach by pre-computing prefix and suffix GCD and LCM arrays. These arrays help us to quickly calculate the GCD and LCM of the remaining elements after removing a single element from the original array. We iterate through the array and for each element, we calculate the factor score after removing that element. We then take the maximum of all these factor scores, including the factor score of the original array (without removing any element).
Step-by-Step Algorithm
- Step 1: Handle the edge case where the array has only one element. In this case, the maximum factor score is the square of that element.
- Step 2: Create four arrays: `pre_gcd`, `suf_gcd`, `pre_lcm`, and `suf_lcm` to store the prefix GCD, suffix GCD, prefix LCM, and suffix LCM, respectively.
- Step 3: Calculate the prefix GCD and prefix LCM arrays. `pre_gcd[i]` stores the GCD of `nums[0]` to `nums[i]`, and `pre_lcm[i]` stores the LCM of `nums[0]` to `nums[i]`.
- Step 4: Calculate the suffix GCD and suffix LCM arrays. `suf_gcd[i]` stores the GCD of `nums[i]` to `nums[n-1]`, and `suf_lcm[i]` stores the LCM of `nums[i]` to `nums[n-1]`.
- Step 5: Initialize `max_score` to the factor score of the original array, which is `pre_gcd[n-1] * pre_lcm[n-1]`.
- Step 6: Iterate through the array `nums`. For each element `nums[i]`, calculate the factor score of the array after removing `nums[i]`.
- Step 7: If `i` is 0, the GCD and LCM are `suf_gcd[1]` and `suf_lcm[1]`, respectively.
- Step 8: If `i` is `n-1`, the GCD and LCM are `pre_gcd[n-2]` and `pre_lcm[n-2]`, respectively.
- Step 9: Otherwise, the GCD is `gcd(pre_gcd[i-1], suf_gcd[i+1])` and the LCM is `lcm(pre_lcm[i-1], suf_lcm[i+1])`.
- Step 10: Update `max_score` with the maximum factor score found so far.
- Step 11: Return `max_score`.
Key Insights
- Insight 1: Pre-compute prefix and suffix GCD and LCM arrays to efficiently calculate GCD and LCM after removing an element.
- Insight 2: The constraints on the input numbers (1 to 30) imply that the numbers are relatively small, therefore the intermediate results of GCD and LCM won't overflow easily, however, the final factor score(product of GCD and LCM) should use a long data type to store the results.
- Insight 3: Consider edge cases where the array has only one element, or when removing the first or last element of the array.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Array, Math, Number Theory.
Companies
Asked at: Info Edge.