Zepruv › Coding problems

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

Starter code (python)

class Solution(object):
    def largestDivisibleSubset(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        

Related problems

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.