Javascript and C++ solutions
Thoughts and Intuition:\n\nWhen I first read this problem, I thought there\'s no way I can check every possible combination without TLE.  So I started looking f
**Thoughts and Intuition:**\n\nWhen I first read this problem, I thought there\'s no way I can check every possible combination without TLE.  So I started looking for a pattern for a potential math solution.  I first noticed example 1 results in `true` with prime numbers `5` and `7` as input, and example 3 results in `false` with non-prime numbers `3` and `6` as input.  However, example 2 results in `true` with non-prime numbers also.  So I thought maybe GCD would work, and indeed it did!  After I solved this problem, I read about [Bezout\'s Identity in this post](https://leetcode.com/problems/check-if-it-is-a-good-array/discuss/419324/Bezout\'s-Identity).\n\n---\n\n*Javascript*\n```\nlet isGoodArray = (A, gcd = (a, b) => !b ? a : gcd(b, a % b)) => {\n    let x = A.shift();\n    A.forEach(y => x = gcd(x, y));\n    return x == 1;\n};\n```\n\n---\n\n*C++*\n```\nclass Solution {\npublic:\n    using VI = vector<int>;\n    bool isGoodArray(VI& A, bool good = false) {\n        auto x = A.back(); A.pop_back();\n        for_each(A.begin(), A.end(), [&](auto y) { x = gcd(x, y); });\n        return x == 1;\n    }\nprivate:\n    int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }\n};\n```
[]