Check If It Is a Good Array — LeetCode 1250 Python Solution

HardArrayMathNumber Theory
Problem
#1250
Reading time
2 min

The 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.

Example

Input
nums = [12,5,7,23]
Output
true
Explanation
Pick numbers 5 and 7.

Python solution

Python
class Solution:
    def isGoodArray(self, nums: List[int]) -> bool:
        return reduce(gcd, nums) == 1

Complexity

MeasureComplexity
TimeO(n + \log m)
SpaceO(1) auxiliary

Pattern: Math and Number Theory

Find the closed form, the invariant, or the modular identity — and skip the loop entirely. LeetCode 1250. Check If It Is a Good Array is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Math and Number Theory.

The math and number theory guide has the Python template for the pattern and the 485 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 1250. Check If It Is a Good Array?
LeetCode 1250. Check If It Is a Good Array is rated Hard on LeetCode.
What is the time complexity of LeetCode 1250. Check If It Is a Good Array?
The Python solution on this page runs in O(n + \log m).
What is the space complexity of LeetCode 1250. Check If It Is a Good Array?
The Python solution on this page uses O(1) auxiliary space.
What topics does LeetCode 1250. Check If It Is a Good Array cover?
LeetCode 1250. Check If It Is a Good Array is tagged Array, Math and Number Theory on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview