Max Score with Brute Force ✅🔥: GCD and LCM Solutions
Approach\n\nJUST FINDING ALL POSSIBLE SOLUTION \n\n# Complexity\n- Time complexity:\n Add your time complexity here, e.g. O(n) \n\n- Space complexity:\n Add you
# Approach\n\nJUST FINDING ALL POSSIBLE SOLUTION \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 []\n#include <vector>\n#include <algorithm>\n#include <numeric>\n\nclass Solution {\npublic:\n    long long get(vector<int>& nums, int x) {\n        long long ans = 0;\n        int n = nums.size();\n\n        long long gcd = (x != 0) ? nums[0] : nums[1];\n        long long lcm = gcd;\n\n        for (int i = (x == 0 ? 2 : 1); i < n; ++i) {\n            if (i == x) continue;\n            int a = nums[i];\n            gcd = __gcd(gcd, (long long)a);\n            lcm = (lcm * a) / __gcd(lcm, (long long)a);\n        }\n\n        return gcd * lcm;\n    }\n\n    long long maxScore(vector<int>& nums) {\n        int n = nums.size();\n        if (n == 1) return (long long)nums[0] * nums[0];\n\n        long long maxi = get(nums, -1);\n\n        for (int i = 0; i < n; ++i) {\n            maxi = max(maxi, get(nums, i));\n        }\n\n        return maxi;\n    }\n};\n\n```\n```javascript []\nclass Solution {\n    gcd(a, b) {\n        return b === 0 ? a : this.gcd(b, a % b);\n    }\n\n    get(nums, x) {\n        let n = nums.length;\n\n        let gcd = (x !== 0) ? nums[0] : nums[1];\n        let lcm = gcd;\n\n        for (let i = (x === 0 ? 2 : 1); i < n; ++i) {\n            if (i === x) continue;\n            let a = nums[i];\n            gcd = this.gcd(gcd, a);\n            lcm = (lcm * a) / this.gcd(lcm, a);\n        }\n\n        return gcd * lcm;\n    }\n\n    maxScore(nums) {\n        let n = nums.length;\n        if (n === 1) return nums[0] * nums[0];\n\n        let maxi = this.get(nums, -1);\n\n        for (let i = 0; i < n; ++i) {\n            maxi = Math.max(maxi, this.get(nums, i));\n        }\n\n        return maxi;\n    }\n}\n\n```\n```python []\nfrom math import gcd\nfrom functools import reduce\n\nclass Solution:\n    def get(self, nums, x):\n        n = len(nums)\n\n        gcd_val = nums[0] if x != 0 else nums[1]\n        lcm_val = gcd_val\n\n        for i in range(2 if x == 0 else 1, n):\n            if i == x:\n                continue\n            a = nums[i]\n            gcd_val = gcd(gcd_val, a)\n            lcm_val = (lcm_val * a) // gcd(lcm_val, a)\n\n        return gcd_val * lcm_val\n\n    def maxScore(self, nums):\n        n = len(nums)\n        if n == 1:\n            return nums[0] * nums[0]\n\n        maxi = self.get(nums, -1)\n\n        for i in range(n):\n            maxi = max(maxi, self.get(nums, i))\n\n        return maxi\n\n```\n```java []\nimport java.util.List;\n\nclass Solution {\n    private long get(List<Integer> nums, int x) {\n        long ans = 0;\n        int n = nums.size();\n\n        long gcd = (x != 0) ? nums.get(0) : nums.get(1);\n        long lcm = gcd;\n\n        for (int i = (x == 0 ? 2 : 1); i < n; ++i) {\n            if (i == x) continue;\n            int a = nums.get(i);\n            gcd = gcd(gcd, a);\n            lcm = (lcm * a) / gcd(lcm, a);\n        }\n\n        return gcd * lcm;\n    }\n\n    public long maxScore(List<Integer> nums) {\n        int n = nums.size();\n        if (n == 1) return (long) nums.get(0) * nums.get(0);\n\n        long maxi = get(nums, -1);\n\n        for (int i = 0; i < n; ++i) {\n            maxi = Math.max(maxi, get(nums, i));\n        }\n\n        return maxi;\n    }\n\n    private long gcd(long a, long b) {\n        return b == 0 ? a : gcd(b, a % b);\n    }\n}\n\n```\n![upvote.jpg](https://assets.leetcode.com/users/images/c556520c-86d7-4431-b7bd-06d676d5216f_1730012647.4307945.jpeg)
['C++', 'Java', 'Python3', 'JavaScript']