Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
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^51 <= nums[i] <= 10^9Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
[12,5,7,23]
[29,6,10]
[3,6]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1250: Check If It Is a Good Array
class Solution {
public boolean isGoodArray(int[] nums) {
int g = 0;
for (int x : nums) {
g = gcd(x, g);
}
return g == 1;
}
private int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
}
// Accepted solution for LeetCode #1250: Check If It Is a Good Array
func isGoodArray(nums []int) bool {
g := 0
for _, x := range nums {
g = gcd(x, g)
}
return g == 1
}
func gcd(a, b int) int {
if b == 0 {
return a
}
return gcd(b, a%b)
}
# Accepted solution for LeetCode #1250: Check If It Is a Good Array
class Solution:
def isGoodArray(self, nums: List[int]) -> bool:
return reduce(gcd, nums) == 1
// Accepted solution for LeetCode #1250: Check If It Is a Good Array
struct Solution;
impl Solution {
fn is_good_array(nums: Vec<i32>) -> bool {
let mut res = nums[0];
let n = nums.len();
for i in 0..n {
res = gcd(res, nums[i]);
if res == 1 {
return true;
}
}
false
}
}
fn gcd(mut m: i32, mut n: i32) -> i32 {
while m != 0 {
let temp = m;
m = n % temp;
n = temp;
}
n.abs()
}
#[test]
fn test() {
let nums = vec![12, 5, 7, 23];
let res = true;
assert_eq!(Solution::is_good_array(nums), res);
let nums = vec![29, 6, 10];
let res = true;
assert_eq!(Solution::is_good_array(nums), res);
let nums = vec![3, 6];
let res = false;
assert_eq!(Solution::is_good_array(nums), res);
}
// Accepted solution for LeetCode #1250: Check If It Is a Good Array
function isGoodArray(nums: number[]): boolean {
return nums.reduce(gcd) === 1;
}
function gcd(a: number, b: number): number {
return b === 0 ? a : gcd(b, a % b);
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.