Java Easy and Clearn Solution ( Explained )
Solution\njava []\nclass Solution {\n    public long gcd(long a, long b) {\n        while(b!=0){\n            long temp =b;\n            b = a%b;\n            a
# Solution\n```java []\nclass Solution {\n    public long gcd(long a, long b) {\n        while(b!=0){\n            long temp =b;\n            b = a%b;\n            a = temp;\n        }\n        return a;\n    }\n\n    public long lcm(long a, long b) {\n        return a*b / gcd(a,b);\n    }\n\n    public long gcdOfArray(int[] arr){\n        long res = arr[0];\n        for(int i=1; i<arr.length;i++){\n            res = gcd(res, arr[i]);\n        }\n        return res;\n    }\n\n    public long lcmOfArray(int[] arr){\n        long res = arr[0];\n        for(int i=1;i<arr.length;i++){\n            res = lcm(res,arr[i]);\n        }\n        return res;\n    }\n\n    public int[] subArray(int[] arr , int index, int n){\n        int[] res = new int[n - 1];\n        for(int i=0, j=0; i<n; i++){\n            if(i==index)continue;\n            res[j++]=arr[i];\n        }\n        return res;\n    }\n\n    public long maxScore(int[] nums) {\n        int n = nums.length;\n        long res = lcmOfArray(nums)*gcdOfArray(nums);\n        if(n==1)return res;\n        for(int i=0;i<n;i++){\n            int[] sub = subArray(nums, i, n);\n            res = Math.max(res,lcmOfArray(sub)*gcdOfArray(sub));\n        }\n        \n        return res;\n    }\n}\n```\n\n### Example Execution\n\nSuppose you call `maxScore` with an array like `[2, 4, 6]`:\n- It computes the LCM and GCD for the entire array.\n- It then generates subarrays such as `[4, 6]`, `[2, 6]`, and `[2, 4]`, calculating their LCMs and GCDs.\n- It compares all computed scores and returns the maximum.\n\n\n### Method Breakdown\n\n1. **`gcd(long a, long b)`**:\n   - This method computes the GCD of two numbers using the Euclidean algorithm.\n   - **How it works**:\n     - While `b` is not zero, it performs the following:\n       - Stores the value of `b` in a temporary variable (`temp`).\n       - Sets `b` to `a % b` (the remainder of `a` divided by `b`).\n       - Updates `a` to the value of `temp` (the previous value of `b`).\n     - When `b` becomes zero, `a` holds the GCD of the two numbers, which is returned.\n\n2. **`lcm(long a, long b)`**:\n   - This method calculates the LCM of two numbers.\n   - **How it works**:\n     - The formula used is \\( \\text{lcm}(a, b) = \\frac{a \\times b}{\\text{gcd}(a, b)} \\).\n     - It first computes the GCD of `a` and `b`, then multiplies `a` and `b`, and divides the product by the GCD to get the LCM.\n\n3. **`gcdOfArray(int[] arr)`**:\n   - This method calculates the GCD of an entire array of integers.\n   - **How it works**:\n     - Initializes a variable `res` with the first element of the array.\n     - Iterates through the rest of the array, updating `res` to the GCD of the current `res` and the next element in the array.\n     - Returns the final GCD of the entire array.\n\n4. **`lcmOfArray(int[] arr)`**:\n   - This method calculates the LCM of an entire array of integers.\n   - **How it works**:\n     - Similar to `gcdOfArray`, it initializes `res` with the first element.\n     - Iterates through the rest of the array, updating `res` to the LCM of the current `res` and the next element.\n     - Returns the final LCM of the entire array.\n\n5. **`subArray(int[] arr, int index, int n)`**:\n   - This method creates a new array that excludes the element at the specified `index`.\n   - **How it works**:\n     - Initializes a new array `res` of size `n - 1` (to account for one fewer element).\n     - Iterates through the original array, copying elements to `res`, skipping the element at `index`.\n     - Returns the resulting subarray.\n\n6. **`maxScore(int[] nums)`**:\n   - This is the main method that calculates the maximum score based on the LCM and GCD of subarrays.\n   - **How it works**:\n     - Initializes `n` as the length of the input array `nums`.\n     - Computes the initial score as the product of the LCM and GCD of the entire array.\n     - If the array has only one element, it returns that score directly.\n     - Iterates through each index of the array, generates a subarray excluding the element at that index, and calculates the LCM and GCD of that subarray.\n     - Updates `res` to be the maximum score found by comparing it to the current score.\n     - Finally, it returns the maximum score.\n\n
['Java']