-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1250.java
More file actions
49 lines (48 loc) · 1.34 KB
/
Copy path1250.java
File metadata and controls
49 lines (48 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// 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
//
// Runtime: 2 ms, faster than 97.64% of Java online submissions for Check If It Is a Good Array.
// Memory Usage: 49 MB, less than 32.28% of Java online submissions for Check If It Is a Good Array.
class Solution {
public boolean isGoodArray(int[] nums) {
int base = nums[0];
for (int i = 0; i < nums.length; i++) {
base = gcd(nums[i], base);
if (base == 1) return true;
}
return false;
}
private int gcd(int a, int b) {
if (a < b) {
return gcd(b, a);
}
return a % b == 0 ? b : gcd(b, a % b);
}
}