# 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`.

Example 1:

``````Input: nums = [12,5,7,23]
Output: true
Explanation: Pick numbers 5 and 7.
5*3 + 7*(-2) = 1
``````

Example 2:

``````Input: nums = [29,6,10]
Output: true
Explanation: Pick numbers 29, 6 and 10.
29*1 + 6*(-3) + 10*(-1) = 1
``````

Example 3:

``````Input: nums = [3,6]
Output: false
``````

Constraints:

• `1 <= nums.length <= 10^5`
• `1 <= nums[i] <= 10^9`

``````class Solution {
public:
bool isGoodArray(vector<int>& nums) {
int res = nums[0];
for (int num : nums) {
res = gcd(res, num);
if (res == 1) return true;
}
return false;
}
};
``````

Github 同步地址:

https://github.com/grandyang/leetcode/issues/1250

https://leetcode.com/problems/check-if-it-is-a-good-array/

https://leetcode.com/problems/check-if-it-is-a-good-array/discuss/1071677/Simple-C%2B%2B-with-Explanation

https://leetcode.com/problems/check-if-it-is-a-good-array/discuss/419368/JavaC%2B%2BPython-Chinese-Remainder-Theorem

LeetCode All in One 题目讲解汇总(持续更新中…)

|

Venmo 打赏

—|—

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation