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:
false
💡 Note:
GCD(29,6,10) = GCD(GCD(29,6),10) = GCD(1,10) = 1. Wait, this should be true. Let me recalculate: GCD(29,6) = 1, GCD(1,10) = 1, so result is true.
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
Understanding the Visualization
1
Input Array
Array of positive integers to analyze
2
GCD Computation
Calculate greatest common divisor of all elements
3
Decision
Return true if GCD equals 1, false otherwise
Key Takeaway
🎯 Key Insight: Use Bézout's identity - an array is good if and only if GCD of all elements equals 1
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code