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

Rust Solution

struct Solution;

impl Solution {
    fn is_good_array(nums: Vec<i32>) -> bool {
        let mut res = nums[0];
        let n = nums.len();
        for i in 0..n {
            res = gcd(res, nums[i]);
            if res == 1 {
                return true;
            }
        }
        false
    }
}

fn gcd(mut m: i32, mut n: i32) -> i32 {
    while m != 0 {
        let temp = m;
        m = n % temp;
        n = temp;
    }
    n.abs()
}

#[test]
fn test() {
    let nums = vec![12, 5, 7, 23];
    let res = true;
    assert_eq!(Solution::is_good_array(nums), res);
    let nums = vec![29, 6, 10];
    let res = true;
    assert_eq!(Solution::is_good_array(nums), res);
    let nums = vec![3, 6];
    let res = false;
    assert_eq!(Solution::is_good_array(nums), res);
}

Having problems with this solution? Click here to submit an issue on github.