Leetcode 1250. Check If It Is a Good Array

Problem Explanation

The task is to find out whether you can make the sum 1 from given positive numbers in an array by multiplying each with an integer.

For instance, if the input array is [12,5,7,23], you could select 5 and 7, and multiply them by 3 and -2 respectively to get the sum of 1. (53 + 7(-2)=1) Therefore, for this case, the result would be true.

For the array [3,6], there are no such integers which can multiply with 3 or 6 to create a sum of 1. Thus, the result here would be false.

The key to solve this problem is to find the greatest common divisor (gcd) among all numbers in the array. The array is a good array if and only if the gcd equals 1. The gcd is the largest value k, that k is a divisor of all numbers in the array. Now, if the gcd equals 1, it means that there exists a way to pick the elements and combine them with positive or negative coefficient such that the sum equals to 1.

Python Solution

1
2python
3import math
4from typing import List
5
6class Solution:
7    def isGoodArray(self, nums: List[int]) -> bool:
8        g = nums[0]
9        for num in nums:
10            g = math.gcd(g, num)   # Find the gcd of all numbers
11        return g == 1              # Check if the gcd equals 1

Java Solution

1
2java
3import java.util.*;
4
5class Solution {
6 public boolean isGoodArray(int[] nums) {
7        int g = nums[0];
8
9        for (int num : nums)
10            g = gcd(g, num);    // Find the gcd of all numbers
11
12        return g == 1;         // Check if the gcd equals 1
13    }
14    
15    public int gcd(int a, int b) {
16        return b == 0 ? a : gcd(b, a % b);  // Function to find gcd of two numbers
17    }
18}

JavaScript Solution

1
2javascript
3class Solution {
4    isGoodArray(nums) {
5        let g = nums[0];
6
7        for (let num of nums)
8            g= this.gcd(g, num);    // Find the gcd of all numbers
9
10        return g === 1;             // Check if the gcd equals 1
11    }
12
13    gcd(a, b) {
14        return b === 0 ? a : this.gcd(b, a % b);  // Function to find gcd of two numbers
15    }
16}

C++ Solution

1
2cpp
3#include <algorithm>
4#include <vector>
5
6class Solution {
7 public:
8  bool isGoodArray(std::vector<int>& nums) {
9    int g = nums[0];
10    for (const int& num : nums)
11      g = std::__gcd(g, num);    // Find the gcd of all numbers
12
13    return g == 1;               // Check if the gcd equals 1
14  }
15};
16

C# Solution

1
2csharp
3using System;
4using System.Linq;
5
6public class Solution {
7    public bool IsGoodArray(int[] nums) {
8        var g = nums[0];
9        foreach (var num in nums){
10            g = gcd(g, num);     // Find the gcd of all numbers
11        }
12        return g == 1;          // Check if the gcd equals 1
13    }
14    
15    private int gcd(int a, int b) {
16        return b == 0 ? a : gcd(b, a % b);   // Function to find gcd of two numbers
17    }
18}

In each of these solutions, we define a method to calculate the greatest common divisor (gcd) of the input array. The function uses an iterative or recursive approach to find the gcd of two numbers. We then iterate through the entire array, calculating the gcd of the current gcd and the next number in the array. If at the end of this process, the gcd is 1, we return true because it is possible to achieve a sum of 1 by multiplying the array elements with certain integers. Otherwise, we return false.

The reasoning behind this approach is the Bézout's identity, which states that the integers a and b have a gcd of 1 if and only if there exist integers x and y such that ax + by = 1. In the context of this problem, the integers a and b are the numbers in the given array and the integers x and y are the multipliers we are trying to find.

By using the built-in gcd functions in Python and C++, and implementing our own in Java, JavaScript and C#, we ensure that our solution is efficient even for large inputs. Each of these solutions has a time complexity of O(n), where n is the number of elements in the input array. This is because we only need to iterate through the array once to find the gcd of its elements.

In conclusion, with a deep understanding of number theory and logic, solving this problem becomes quite straightforward. These programs can be easily integrated into larger systems or used as standalone applications. They can also be used as a basis for solving other similar array and number theory problems.


Got a question? Ask the Teaching Assistant anything you don't understand.

Still not clear? Ask in the Forum,  Discord or Submit the part you don't understand to our editors.


TA 👨‍🏫