Java, Simple Using GCD🌻
Intuition\nThe problem seems to involve finding the greatest common divisor (GCD) of elements in the given array.\n\n# Approach\nThe code defines a gcd method t
# Intuition\nThe problem seems to involve finding the greatest common divisor (GCD) of elements in the given array.\n\n# Approach\nThe code defines a gcd method to calculate the GCD of two numbers using recursion. Then, the isGoodArray method initializes a variable result with the first element of the array and iteratively calculates the GCD of result and the next element in the array. If at any point the GCD becomes 1, it returns true. Finally, the method checks if the final GCD is 1 and returns true if so, otherwise false.\n\n# Complexity\n- Time complexity:\nThe time complexity of the gcd function is typically O(log(min(a, b))), and the loop in the isGoodArray method iterates through the array once. So, the overall time complexity is O(n log(min(a, b))), where n is the length of the array.\n\n- Space complexity:\nThe space complexity is O(1) as there is no additional space used that scales with the input.\n\n# Code\n```\nclass Solution {\n\n    public int gcd(int a, int b)\n    {\n        if (b==0) return a;\n\n        if (a>b)\n        return gcd(b , a%b);\n\n        else \n        return gcd(a, b%a);\n    }\n\n\n    public boolean isGoodArray(int[] nums) {\n        \n        int result = nums[0];\n\n        for (int i=1;i<nums.length;i++)\n        {\n            if (result==1) return true;\n\n            else result = gcd(result, nums[i]);\n        }\n\n        if (result==1) return true;\n        return false;\n    }\n}\n```
['Java']