Talentd
FleetCode

1250. Check If It Is a Good Array

Hard65.6% acceptance5 companies

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

Hints open one at a time

Hint 1
Eq. ax+by=1 has solution x, y if gcd(a,b) = 1.
Hint 2
Can you generalize the formula?. Check Bézout's lemma.
4:17
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def isGoodArray(self, nums: List[int]) -> bool:
3
Starter codePython 3Spaces: 4
Examples
Input
[12,5,7,23]
Input
[29,6,10]
Input
[3,6]
Your code is saved on this device.Solution