C++ Solution | Time Complexity: O(n) | Space Complexity: O(n) | Beats 100% Runtime
IntuitionThe first thing that would come to your mind when thinking about removing at most one element is the need to link the elements before and after it. To
# Intuition
The first thing that would come to your mind when thinking about removing at most one element is the need to link the elements before and after it. To do that, I need to know the values of all elements after it and all elements before it, and then concatenate these values.
# Approach
<!-- Describe your approach to solving the problem. -->
The best way to achieve this is by using prefix and suffix arrays. This allows me to calculate the necessary values efficiently. I will create four arrays: two for the prefix GCD and LCM, and two for the suffix GCD and LCM. Then, for each element, I calculate the GCD between the values before and after it, and maximize the result accordingly

Take care of the following cases:

1-If the array size is one, it could cause a runtime error, so handle this case early and exit gracefully.

2-If no element is removed at all, take the full GCD and LCM of the array only once. I made the mistake of calculating it twice, so avoid that.

3-Handle edge element removing, such as removing the first or last element. I addressed these cases while initializing the ans variable.
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
we used five loops each is n so its O(n)
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
we used 4 vectors so its also O(n)
# Code
```cpp []
class Solution {
public:
    long long maxScore(vector<int>& nums) {
        if(nums.size()==1)return nums[0]*nums[0];
        vector<long long> GcdPre(nums.size()),LcmPre(nums.size()),GcdSuf(nums.size()),LcmSuf(nums.size());
        GcdPre[0]=nums[0];
        LcmPre[0]=nums[0];
        GcdSuf[nums.size()-1]=nums[nums.size()-1];   
        LcmSuf[nums.size()-1]=nums[nums.size()-1];
        for(int i = 1 ; i <nums.size();i++)
        {
            GcdPre[i]=__gcd(GcdPre[i-1],nums[i]*1ll);
        }
        for(int i = 1 ; i <nums.size();i++)
        {
            LcmPre[i]=(LcmPre[i-1]*nums[i]*1ll)/__gcd(LcmPre[i-1],nums[i]*1ll);
        }
        for(int i = nums.size()-2 ; i >=0;i--)
        {
            GcdSuf[i]=__gcd(GcdSuf[i+1],nums[i]*1ll);
        }  
        for(int i = nums.size()-2 ; i >=0;i--)
        {
            LcmSuf[i]=(LcmSuf[i+1]*nums[i]*1ll)/__gcd(LcmSuf[i+1],nums[i]*1ll);
        }
        long long ans = max({GcdPre[nums.size()-1]*LcmPre[nums.size()-1],GcdSuf[0]*LcmSuf[0],GcdPre[nums.size()-2]*LcmPre[nums.size()-2],GcdSuf[1]*LcmSuf[1]});
        for(int i =1 ; i < nums.size()-1;i++)
        {
            ans = max(ans,__gcd(GcdPre[i-1],GcdSuf[i+1])*((LcmSuf[i+1])/__gcd(LcmSuf[i+1],LcmPre[i-1])*LcmPre[i-1]));
        }  
        return ans ;
    }
};
```
['C++']