Check If It Is a Good Array - Problem
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.
Input & Output
Example 1 — Good Array
$
Input:
nums = [12,5,7,23]
›
Output:
true
💡 Note:
GCD(12,5,7,23) = GCD(GCD(GCD(12,5),7),23) = GCD(GCD(1,7),23) = GCD(1,23) = 1. Since GCD equals 1, we can form sum of 1 using integer combinations.
Example 2 — Not Good Array
$
Input:
nums = [29,6,10]
›
Output:
true
💡 Note:
GCD(29,6,10) = GCD(GCD(29,6),10) = GCD(1,10) = 1. Since GCD equals 1, we can form sum of 1 using integer combinations.
Example 3 — All Even Numbers
$
Input:
nums = [4,6,10,12]
›
Output:
false
💡 Note:
All numbers are even, so GCD(4,6,10,12) = 2 ≠ 1. Cannot form sum of 1 since all combinations will be even.
Constraints
- 1 ≤ nums.length ≤ 103
- 1 ≤ nums[i] ≤ 109
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code