diff options
Diffstat (limited to 'check_good_array.py')
-rw-r--r-- | check_good_array.py | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/check_good_array.py b/check_good_array.py new file mode 100644 index 0000000..b223b12 --- /dev/null +++ b/check_good_array.py @@ -0,0 +1,25 @@ +# 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' +""" + + +class Solution(object): + def isGoodArray(self, nums): + gcd = nums[0] + for i in nums: + while i: + gcd, i = i, gcd % i + return gcd == 1 + + +if __name__ == "__main__": + obj = Solution() + print(obj.isGoodArray([12, 5, 7, 23])) # expect: True + print(obj.isGoodArray([29, 6, 10])) # expect: True + print(obj.isGoodArray([3, 6])) # expect: False |