Find the Maximum Factor Score of Array - Problem

You're given an integer array nums and need to find the maximum factor score possible after removing at most one element.

The factor score of an array is calculated as: GCD × LCM of all elements in the array.

Key Rules:

  • For a single number, both GCD and LCM equal the number itself
  • For an empty array, the factor score is 0
  • You can remove at most one element (including removing none)

Example: For array [12, 6, 4]:
• Original: GCD(12,6,4) = 2, LCM(12,6,4) = 12, Score = 24
• Remove 4: GCD(12,6) = 6, LCM(12,6) = 12, Score = 72
• Remove 6: GCD(12,4) = 4, LCM(12,4) = 12, Score = 48
• Remove 12: GCD(6,4) = 2, LCM(6,4) = 12, Score = 24

The maximum factor score is 72.

Input & Output

example_1.py — Basic Case
$ Input: nums = [2, 4, 8, 16]
Output: 64
💡 Note: Original array: GCD=2, LCM=16, score=32. Remove 2: GCD=4, LCM=16, score=64. Remove 4: GCD=2, LCM=16, score=32. Remove 8: GCD=2, LCM=16, score=32. Remove 16: GCD=2, LCM=8, score=16. Maximum score is 64.
example_2.py — Single Element
$ Input: nums = [3]
Output: 9
💡 Note: For a single element, both GCD and LCM equal the element itself. Factor score = 3 × 3 = 9. Removing the only element gives score 0, so maximum is 9.
example_3.py — All Same Elements
$ Input: nums = [5, 5, 5]
Output: 25
💡 Note: When all elements are the same, GCD=LCM=5 regardless of removal. Original: 5×5=25. Remove any: 5×5=25. Remove two: 5×5=25. All scenarios give the same score of 25.

Constraints

  • 1 ≤ nums.length ≤ 100
  • 1 ≤ nums[i] ≤ 109
  • All elements are positive integers
  • Time limit: 2 seconds

Visualization

Tap to expand
🎵 Band Harmony OptimizationOriginal Band: [12, 6, 4]1264GCD=2, LCM=12Harmony = 24Remove Member 12:1264GCD=2, LCM=12Harmony = 24Remove Member 6:1264GCD=4, LCM=12Harmony = 48Remove Member 4:1264GCD=6, LCM=12Harmony = 72 ⭐💡 Optimization Strategy1. Brute Force: O(n²) - try every removal2. Prefix-Suffix: O(n) - precompute segmentsprefix[i-1]skip[i]suffix[i+1]Combine prefix and suffix to get resultwithout element i in O(1) time!Time: O(n) vs O(n²)
Understanding the Visualization
1
Assess Current Band
Calculate the harmony score of the full band: GCD × LCM of all members
2
Try Removing Each Member
For each member, calculate what the band's harmony would be without them
3
Optimize with Prefix-Suffix
Instead of recalculating from scratch, use precomputed harmony values from left and right segments
4
Find Maximum Harmony
Compare all scenarios (original band and each removal) to find the best configuration
Key Takeaway
🎯 Key Insight: Instead of recalculating GCD/LCM from scratch for each removal, use prefix-suffix arrays to combine precomputed segments in O(1) time, reducing overall complexity from O(n²) to O(n).
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
43.7K Views
Medium-High Frequency
~25 min Avg. Time
1.8K 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