Skip to content

Commit 35418c7

Browse files
authored
Create check-if-it-is-a-good-array.cpp
1 parent 32c418d commit 35418c7

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

C++/check-if-it-is-a-good-array.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Time: O(nlogn)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
bool isGoodArray(vector<int>& nums) {
7+
// Bézout's identity
8+
int result = nums[0];
9+
for (const auto& num : nums) {
10+
result = std::gcd(result, num); // built-in gcd since C++17, O(logn)
11+
if (result == 1) {
12+
break;
13+
}
14+
}
15+
return result == 1;
16+
}
17+
};

0 commit comments

Comments
 (0)