Check If It Is a Good Array — LeetCode 1250 Python Solution
- Problem
- #1250
- Pattern
- Math and Number Theory
- Reading time
- 2 min
- Source
- leetcode.com
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
class Solution:
def isGoodArray(self, nums: List[int]) -> bool:
return reduce(gcd, nums) == 1Complexity
| Measure | Complexity |
|---|---|
| Time | O(n + \log m) |
| Space | O(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.