Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Largest Divisible Subset

XPChallenge Points: 20
levelLevel: Medium

You are given a list of distinct positive integers nums.
Find the largest subset (let’s call it ans) such that for every pair (a, b) in ans:


• a % b == 0 or b % a == 0
If more than one subset has the same maximum size, return any one of them.


Example 1:

Input: nums = [1, 2, 3]

Output: [1, 2] or [1, 3]

Explanation:

• In [1, 2], 2 % 1 = 0 • In [1, 3], 3 % 1 = 0

Example 2:

Input: nums = [1, 2, 4, 8]

Output: [1, 2, 4, 8]

Explanation:

Each number divides the next.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result