Calculate Prefix and Suffix for both lcm and gcd
Intuition\nWe can calculate prefix and suffix arrays for gcd and lcm as they\'re both associative.\n\n# Approach\n- For array size n \n  - if n = 0 return 0 as 
# Intuition\nWe can calculate prefix and suffix arrays for gcd and lcm as they\'re both associative.\n\n# Approach\n- For array size $$n$$ \n  - if $$n = 0$$ return $$0$$ as mentioned in the problem\n  - Else if $$n = 1$$ return $$nums[0] * nums[0]$$ as $$nums[0]$$ as lcm and gcd of a number is itself\n  - Else, proceed with the steps below\n- Calculate prefix and suffix arrays for lcm and gcd\n- Calculate the result without removing any elements as\n  - $$postlcm[0] * postgcd[0]$$ or $$prelcm[n-1] * pregcd[n - 1]$$\n- Calculate value without index for $$i \\text{ for } i = 1, \\ldots, n - 2$$ \n  - $$lcm = lcm(prelcm[i - 1], postlcm[i + 1])$$\n  - $$gcd = gcd(pregcd[i - 1], postgcd[i + 1])$$\n  - $$value = lcm * gcd$$\n- For $$i = 0$$ or $$i = n - 1$$\n  - $$i = 0$$: $$postlcm[1] * postgcd[1]$$\n  - $$i = n - 1$$: $$prelcm[n - 2] * pregcd[n - 2]$$\n\n# Complexity\n- Time complexity: $$O(n)$$\n\n- Space complexity: $$O(n)$$\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    long long maxScore(vector<int>& nums) {\n        int n = nums.size();\n        if (n == 0) return 0;\n        if (n == 1) return nums[0] * nums[0];\n\n        vector<int> pregcd(n, 0), postgcd(n, 0);\n        vector<long long> prelcm(n, 0), postlcm(n, 0);\n        pregcd[0] = prelcm[0] = nums[0];\n\n        --n;\n        for (int i = 1; i <= n; ++i) {\n            pregcd[i] = gcd(nums[i], pregcd[i - 1]);\n            prelcm[i] = lcm(prelcm[i - 1], nums[i]);\n        }\n        \n        postgcd[n] = postlcm[n] = nums[n];\n        for (int i = n - 1; i >= 0; --i) { \n            postgcd[i] = gcd(nums[i], postgcd[i + 1]);\n            postlcm[i] = lcm(postlcm[i + 1], nums[i]);\n        }\n        \n        long long res = max(\n            max(postlcm[0] * postgcd[0], postlcm[1] * postgcd[1]),\n            prelcm[n - 1] * pregcd[n - 1]\n        );\n        \n        long long cur;\n        for (int i = 1; i < n; ++i) {\n            cur = lcm(prelcm[i - 1], postlcm[i + 1]) *\n                    gcd(pregcd[i - 1], postgcd[i + 1]);\n            res = max(cur, res);\n        }\n\n        return res;\n    }\n};\n```
['C++']