[LeetCode] Check If It Is a Good Array

1250. Check If It Is a Good Array

Given an array nums of positive integers. Your task is to select some subset of nums, multiply each element by an integer and add all these numbers. The array is said to be good if you can obtain a sum of 1 from the array by any possible subset and multiplicand.

Return True if the array is good otherwise return False.

1
2
3
4
5
6
7
8
9
10
class Solution {
public:
bool isGoodArray(vector<int>& nums) {
int g = nums[0];
for(int i = 1; i < nums.size() and g != 1; i++) {
g = __gcd(g, nums[i]);
}
return g == 1;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/04/01/PS/LeetCode/check-if-it-is-a-good-array/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.