Largest Divisible Subset
Medium · Array, Math, Dynamic Programming, Sorting
Given a set of distinct positive integers nums, return the largest subset answer such that every pair (answer[i], answer[j]) of elements in this subset satisfies:
If there are multiple solutions, return any of them.
Example 1
Input: nums = [1,2,3] Output: [1,2]
[1,3] is also accepted.
Example 2
Input: nums = [1,2,4,8] Output: [1,2,4,8]
Constraints
- 1 <= nums.length <= 1000
- 1 <= nums[i] <= 2 * 109
- All the integers in nums are unique.
Starter code (python)
class Solution(object):
def largestDivisibleSubset(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
Related problems
- Two Sum (Easy)
- Add Two Numbers (Medium)
- Median of Two Sorted Arrays (Hard)
Previous: Valid Perfect Square · Next: Sum of Two Integers · All problems
Zepruv is a technical interview practice platform. Read any problem for free; sign in to run your code, submit and keep your progress.