C++ | Brute Force Solution | Clean Code ✅ | Beginner Friendly
Time complexity is\n    - due to outer loop = N\n    - due to inner loop = N\n    - for gcd function = log(min(a, b)) , since a and b can be at max 30, the comp
Time complexity is\n    - due to outer loop = N\n    - due to inner loop = N\n    - for gcd function = log(min(a, b)) , since a and b can be at max 30, the complexity becomes =~ log 30 =~ constant\n    \n    TC - O(N^2)\n    SC - O(1)\n\n# Code\n```cpp []\nclass Solution {\npublic:\n\n    // optimized Euclidean algorithm for GCD \n    long long getGCD(long long a, long long b){\n\n        if(a==0) return b;\n        if(b==0) return a;\n\n        if(a>b) return getGCD(a%b, b);\n\n        return getGCD(a, b%a);\n    }\n\n    long long getLCM(long long a, long long b){\n        if(a==b) return a;\n        return (a*b)/getGCD(a, b);\n    }\n\n    long long maxScore(vector<int>& nums) {\n\n        if(nums.size() == 1) return nums[0]*nums[0];\n\n        long long maxFactor = 0;\n\n        // i is for tracking the element to be removed. In other words, we\'ll remove the (i-1)th number from array\n        // j is to loop over the array\n        for(int i=0; i<=nums.size(); i++){\n            \n            // initialize lcm and gcd\n            long long lcm, gcd;\n            if(i==1) {lcm = nums[1]; gcd = nums[1];}\n            else {lcm = nums[0]; gcd = nums[0];}\n\n            for(int j=0; j<nums.size(); j++){\n                \n                if(i-1==j) continue; // don\'t consider i-1th element\n\n                lcm = getLCM(lcm, nums[j]);\n                gcd = getGCD(gcd, nums[j]);\n                \n            }\n\n            maxFactor = max(maxFactor, lcm*gcd);\n        }\n\n        return maxFactor;\n    }\n\n};\n```
['Math', 'Number Theory', 'C++']