C++ | Prefix Suffix
Code\ncpp []\nclass Solution {\npublic:\n    long long maxScore(vector<int>& nums) {\n        int n = nums.size();\n        if(n == 1) return nums[0] * nums[0];
# Code\n```cpp []\nclass Solution {\npublic:\n    long long maxScore(vector<int>& nums) {\n        int n = nums.size();\n        if(n == 1) return nums[0] * nums[0];\n        vector<long long> gcdRight(n), lcmRight(n);\n        gcdRight[n - 1] = lcmRight[n - 1] = nums[n - 1];\n        long long ans = 0;\n        for(int i = n - 2; i >= 0; i--) {\n            gcdRight[i] = gcd(gcdRight[i + 1], nums[i]);\n            lcmRight[i] = lcm(lcmRight[i + 1], nums[i]);\n        }\n        long long gcdLeft = nums[0], lcmLeft = nums[0];\n        for(int i = 1; i < n - 1; i++) {\n            long long g = gcd(gcdLeft, gcdRight[i + 1]);\n            long long l = lcm(lcmLeft, lcmRight[i + 1]);\n            gcdLeft = gcd(gcdLeft, nums[i]);\n            lcmLeft = (lcmLeft *  nums[i]) / gcd(lcmLeft, nums[i]);\n            ans = max(ans, l * g);\n        }\n        // Remove leftmost\n        ans = max(ans, gcdRight[1] * lcmRight[1]);\n        // Remove none\n        ans = max(ans, gcdRight[0] * lcmRight[0]);\n        // Remove rightmost\n        ans = max(ans, gcdLeft * lcmLeft);\n        return ans;\n    }\n};\n```
['C++']