Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 maximum factor score of 4 * 16 = 64.
Example 2:
Input: nums = [1,2,3,4,5]
Output: 60
Explanation:
The maximum factor score of 60 can be obtained without removing any elements.
Example 3:
Input: nums = [3]
Output: 9
Constraints:
1 <= nums.length <= 1001 <= nums[i] <= 30Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
[2,4,8,16]
[1,2,3,4,5]
[3]
greatest-common-divisor-of-strings)remove-one-element-to-make-the-array-strictly-increasing)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3334: Find the Maximum Factor Score of Array
class Solution {
public long maxScore(int[] nums) {
int n = nums.length;
long[] sufGcd = new long[n + 1];
long[] sufLcm = new long[n + 1];
sufLcm[n] = 1;
for (int i = n - 1; i >= 0; --i) {
sufGcd[i] = gcd(sufGcd[i + 1], nums[i]);
sufLcm[i] = lcm(sufLcm[i + 1], nums[i]);
}
long ans = sufGcd[0] * sufLcm[0];
long preGcd = 0, preLcm = 1;
for (int i = 0; i < n; ++i) {
ans = Math.max(ans, gcd(preGcd, sufGcd[i + 1]) * lcm(preLcm, sufLcm[i + 1]));
preGcd = gcd(preGcd, nums[i]);
preLcm = lcm(preLcm, nums[i]);
}
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;
}
}
// Accepted solution for LeetCode #3334: Find the Maximum Factor Score of Array
func maxScore(nums []int) int64 {
n := len(nums)
sufGcd := make([]int64, n+1)
sufLcm := make([]int64, n+1)
sufLcm[n] = 1
for i := n - 1; i >= 0; i-- {
sufGcd[i] = gcd(sufGcd[i+1], int64(nums[i]))
sufLcm[i] = lcm(sufLcm[i+1], int64(nums[i]))
}
ans := sufGcd[0] * sufLcm[0]
preGcd, preLcm := int64(0), int64(1)
for i := 0; i < n; i++ {
ans = max(ans, gcd(preGcd, sufGcd[i+1])*lcm(preLcm, sufLcm[i+1]))
preGcd = gcd(preGcd, int64(nums[i]))
preLcm = lcm(preLcm, int64(nums[i]))
}
return ans
}
func gcd(a, b int64) int64 {
if b == 0 {
return a
}
return gcd(b, a%b)
}
func lcm(a, b int64) int64 {
return a / gcd(a, b) * b
}
# Accepted solution for LeetCode #3334: Find the Maximum Factor Score of Array
class Solution:
def maxScore(self, nums: List[int]) -> int:
n = len(nums)
suf_gcd = [0] * (n + 1)
suf_lcm = [0] * n + [1]
for i in range(n - 1, -1, -1):
suf_gcd[i] = gcd(suf_gcd[i + 1], nums[i])
suf_lcm[i] = lcm(suf_lcm[i + 1], nums[i])
ans = suf_gcd[0] * suf_lcm[0]
pre_gcd, pre_lcm = 0, 1
for i, x in enumerate(nums):
ans = max(ans, gcd(pre_gcd, suf_gcd[i + 1]) * lcm(pre_lcm, suf_lcm[i + 1]))
pre_gcd = gcd(pre_gcd, x)
pre_lcm = lcm(pre_lcm, x)
return ans
// Accepted solution for LeetCode #3334: Find the Maximum Factor Score of Array
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3334: Find the Maximum Factor Score of Array
// class Solution {
// public long maxScore(int[] nums) {
// int n = nums.length;
// long[] sufGcd = new long[n + 1];
// long[] sufLcm = new long[n + 1];
// sufLcm[n] = 1;
// for (int i = n - 1; i >= 0; --i) {
// sufGcd[i] = gcd(sufGcd[i + 1], nums[i]);
// sufLcm[i] = lcm(sufLcm[i + 1], nums[i]);
// }
// long ans = sufGcd[0] * sufLcm[0];
// long preGcd = 0, preLcm = 1;
// for (int i = 0; i < n; ++i) {
// ans = Math.max(ans, gcd(preGcd, sufGcd[i + 1]) * lcm(preLcm, sufLcm[i + 1]));
// preGcd = gcd(preGcd, nums[i]);
// preLcm = lcm(preLcm, nums[i]);
// }
// 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;
// }
// }
// Accepted solution for LeetCode #3334: Find the Maximum Factor Score of Array
function maxScore(nums: number[]): number {
const n = nums.length;
const sufGcd: number[] = Array(n + 1).fill(0);
const sufLcm: number[] = Array(n + 1).fill(1);
for (let i = n - 1; i >= 0; i--) {
sufGcd[i] = gcd(sufGcd[i + 1], nums[i]);
sufLcm[i] = lcm(sufLcm[i + 1], nums[i]);
}
let ans = sufGcd[0] * sufLcm[0];
let preGcd = 0,
preLcm = 1;
for (let i = 0; i < n; i++) {
ans = Math.max(ans, gcd(preGcd, sufGcd[i + 1]) * lcm(preLcm, sufLcm[i + 1]));
preGcd = gcd(preGcd, nums[i]);
preLcm = lcm(preLcm, nums[i]);
}
return ans;
}
function gcd(a: number, b: number): number {
return b === 0 ? a : gcd(b, a % b);
}
function lcm(a: number, b: number): number {
return (a / gcd(a, b)) * b;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.