Good and Easy to understand solution using prefix and suffix gcd and lcm arrays
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    long long gcd(long long a, long long b) {\n        return b == 0 ? a : gcd(b, a % b);\n    }\n\n    long long lcm(long long a, long long b) {\n        return (a / gcd(a, b)) * b; \n    }\n\n    long long maxScore(vector<int>& nums) {\n        // step 1. make gcd prefix suffix\n        // step 2. make lcm prefix suffix \n        // step 3. loop in element and find max score excluding that element\n        int n = nums.size();\n        if (n == 1) return (long long)nums[0] * nums[0]; \n\n        vector<int> gcdp(n), gcds(n);\n        vector<long long> lcmp(n), lcms(n);\n\n        // Prefix GCD\n        gcdp[0] = nums[0];\n        for (int i = 1; i < n; i++) {\n            gcdp[i] = gcd(gcdp[i - 1], nums[i]);\n        }\n\n        // Suffix GCD\n        gcds[n - 1] = nums[n - 1];\n        for (int i = n - 2; i >= 0; i--) {\n            gcds[i] = gcd(gcds[i + 1], nums[i]);\n        }\n\n        // Prefix LCM\n        lcmp[0] = nums[0];\n        for (int i = 1; i < n; i++) {\n            lcmp[i] = lcm(lcmp[i - 1], nums[i]);\n        }\n\n        // Suffix LCM\n        lcms[n - 1] = nums[n - 1];\n        for (int i = n - 2; i >= 0; i--) {\n            lcms[i] = lcm(lcms[i + 1], nums[i]);\n        }\n\n        long long res = lcmp[n - 1] * gcdp[n - 1]; // Initial max score\n\n        // Loop to find max score by excluding each element\n        for (int i = 0; i < n; i++) {\n            int remgcd;\n            long long remlcm;\n\n            if (i == 0) {\n                remgcd = gcds[1];\n                remlcm = lcms[1];\n            } else if (i == n - 1) {\n                remgcd = gcdp[n - 2];\n                remlcm = lcmp[n - 2];\n            } else {\n                remgcd = gcd(gcdp[i - 1], gcds[i + 1]);\n                remlcm = lcm(lcmp[i - 1], lcms[i + 1]);\n            }\n\n            res = max(res, remgcd * remlcm);\n        }\n\n        return res;\n    }\n};\n\n```
['C++']