Maximize Factor Score of an Array After Removing One Element
Intuition\n Describe your first thoughts on how to solve this problem. \nThe factor score is defined as the product of the LCM and GCD of the array. Removing on
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe factor score is defined as the product of the LCM and GCD of the array. Removing one element from the array may increase the factor score by changing the GCD and LCM in ways that maximize their product. We aim to explore all possible configurations by either keeping the entire array or removing one element at a time.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Initial Calculations:\n- Calculate the GCD and LCM for the entire array.\n- Store their product as the initial factor score.\n\n2. Removing Elements:\n- For each element, create a new array excluding that element.\n- Compute the GCD and LCM of the new array.\n- Calculate the factor score (GCD * LCM) for the new array and update the maximum score if it is higher than the previous maximum.\n\n3. Return the Maximum Score:\nAfter iterating through all configurations (including no removal), return the highest factor score obtained.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nThe time complexity is $$O(n^2)$$, where n is the length of the array. This is because we calculate the GCD and LCM of a new array of length $$n \u2212 1$$ for each element removed. The `reduce` calls for each of these configurations make this approach quadratic.\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nThe space complexity is $$O(n)$$, primarily due to the storage of intermediate arrays and calculations for GCD and LCM.\n# Code\n```javascript []\n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar maxScore = function(nums) {\n    if (nums.length === 1) {\n        return nums[0] * nums[0];\n    }\n\n    const totalGCD = calculateGCDOfArray(nums);\n    const totalLCM = calculateLCMOfArray(nums);\n    let maxScore = totalGCD * totalLCM;\n\n    for (let i = 0; i < nums.length; i++) {\n        const newNums = nums.slice(0, i).concat(nums.slice(i + 1));\n        const currentGCD = calculateGCDOfArray(newNums);\n        const currentLCM = calculateLCMOfArray(newNums);\n        const currentScore = currentGCD * currentLCM;\n        \n        maxScore = Math.max(maxScore, currentScore);\n    }\n\n    return maxScore;\n};\n\nfunction gcd(a, b) {\n    while (b !== 0) {\n        [a, b] = [b, a % b];\n    }\n    return a;\n}\n\nfunction lcm(a, b) {\n    return (a * b) / gcd(a, b);\n}\n\nfunction calculateGCDOfArray(arr) {\n    return arr.reduce((acc, num) => gcd(acc, num));\n}\n\nfunction calculateLCMOfArray(arr) {\n    return arr.reduce((acc, num) => lcm(acc, num));\n}\n```
['JavaScript']