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
Good Array Problem: Can We Make Sum = 1?Input Array[12, 5, 7, 23]Compute GCDGCD(12,5,7,23)ResultGCD = 1 → trueMathematical Insight: Bézout's IdentityIf GCD(a₁, a₂, ..., aₙ) = d, then integers exist such thatx₁a₁ + x₂a₂ + ... + xₙaₙ = dWe need sum = 1, so we need GCD = 1Time: O(n log min) | Space: O(1)
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
Asked in
Google 12 Microsoft 8
28.4K Views
Medium Frequency
~15 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen