Find the Maximum Factor Score of Array - Problem

You are given an integer array nums. The factor score of an array is defined as the product of the LCM (Least Common Multiple) and GCD (Greatest Common Divisor) of all elements of that array.

Return the maximum factor score of nums after removing at most one element from it.

Note:

  • Both the LCM and GCD of a single number are the number itself
  • The factor score of an empty array is 0

Input & Output

Example 1 — Powers of 2
$ Input: nums = [2,4,8,16]
Output: 64
💡 Note: Removing the first element (2) gives us [4,8,16]. GCD(4,8,16) = 4, LCM(4,8,16) = 16, so factor score = 4 × 16 = 64. This is the maximum possible.
Example 2 — Small Array
$ Input: nums = [1,2]
Output: 4
💡 Note: Original array: GCD(1,2) = 1, LCM(1,2) = 2, score = 2. Remove 1: [2] gives score = 2×2 = 4. Remove 2: [1] gives score = 1×1 = 1. Maximum is 4.
Example 3 — Single Element
$ Input: nums = [3]
Output: 9
💡 Note: Single element: GCD(3) = 3, LCM(3) = 3, factor score = 3 × 3 = 9. Removing the element gives an empty array with score 0. Maximum is 9.

Constraints

  • 1 ≤ nums.length ≤ 100
  • 1 ≤ nums[i] ≤ 106

Visualization

Tap to expand
Find Maximum Factor Score of ArrayFactor Score = GCD × LCM24816Original: GCD=2, LCM=16, Score=32Remove 2:4816Score = 64 ✓Remove 4:2816Score = 32Maximum Factor Score64
Understanding the Visualization
1
Input Array
Given array [2,4,8,16] with factor score = GCD×LCM
2
Try Removals
Test removing each element and calculate new factor scores
3
Maximum Found
Return the highest factor score: 64
Key Takeaway
🎯 Key Insight: Removing an element that reduces the GCD more than it reduces the LCM can increase the overall factor score
Asked in
Google 35 Meta 28 Microsoft 22 Amazon 18
23.4K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen