Largest Divisible Subset
Largest Divisible Subset is LeetCode 368 (Medium). You get an array nums of distinct positive integers. Return the largest subset in which every pair (a, b) satisfies a % b == 0 or b % a == 0. If several subsets have the same size, any of them is accepted.
- There are at most 1,000 numbers, so checking every pair (about 500,000 checks) is fine.
- Trying every subset is not: 1,000 numbers have 2¹⁰⁰⁰ subsets.
- 1 <= nums.length <= 1000
- 1 <= nums[i] <= 2 * 10⁹
- All the integers in nums are unique.
Intuition
Sort the numbers. In sorted order, a valid subset is a chain where each number divides the next one, like 1 → 2 → 4 → 8.
- Why a chain is enough: division carries through. If
2divides4and4divides8, then2divides8. So checking neighbours in the chain covers every pair. - Why every valid subset is a chain: in a valid subset, the smaller of any two numbers divides the larger, so each number divides the next one in sorted order.
That turns the problem into "find the longest chain", the same shape as Longest Increasing Subsequence with "divides" in place of "is smaller than".
A pairwise rule that carries through (if a relates to b and b to c, then a relates to c), plus a request for the biggest group, points to sorting and then a longest-chain DP. Remember each element’s predecessor whenever the group itself, not just its size, must be returned.
Approach
Before reading on, sort [9, 2, 4, 3, 8] and write, for each number, the longest chain that ends at it. Which number ends the longest chain, and is it the largest one?
Sort the numbers
Sort nums in ascending order. A smaller number can divide a bigger one but never the reverse, so after sorting each number only needs to be tested against the numbers before it, with nums[i] % nums[j] == 0.
Store the best chain ending at each number
Let dp[i] be the length of the longest chain whose last number is nums[i], and parent[i] the index of the number just before it. Every number starts as a chain of length 1 with no parent, because a number on its own is a valid subset.
Try every earlier divisor
For each i, look at every j < i. If nums[j] divides nums[i] and dp[j] + 1 > dp[i], set dp[i] = dp[j] + 1 and parent[i] = j. Adding nums[i] after nums[j] is safe, since nums[j] divides nums[i] and so does everything before nums[j] in that chain.
Track where the longest chain ends
Keep the index whose dp is largest while you fill the table. The longest chain can end before the largest number: in [2, 3, 4, 8, 9] it ends at 8, not 9.
Rebuild the subset
Start at the best index and follow parent links until one is -1, collecting each number. The numbers come out from largest to smallest, so reverse them before returning (any order is accepted, but sorted reads best).
Code in Python, C++ and Java
Common pitfalls
Testing divisibility the wrong way round
if nums[j] % nums[i] == 0:
if nums[i] % nums[j] == 0:
After sorting, nums[j] is the smaller number, and a smaller positive number is never a multiple of a bigger one. On [1, 2, 4, 8] no chain ever grows, and the function returns [1] instead of [1, 2, 4, 8].
Skipping the sort
dp = [1] * n # nums used as given
nums.sort() dp = [1] * n
The loop only looks left for divisors. On [4, 2, 1] every divisor sits to the right of its multiple, so nothing is chained and the answer is [4] instead of [1, 2, 4].
Assuming the chain ends at the last number
idx = n - 1
if dp[i] > dp[best_idx]:
best_idx = iOn [1, 2, 4, 7] the last number 7 only ends the chain [1, 7], while [1, 2, 4] is longer. Starting the rebuild at the last index returns the shorter subset.
Edge cases
[2, 3, 5]Every dp value stays 1 and the best index stays 0, so the answer is [2]. Any single number is a correct answer.
[7]The loops do not run, and the rebuild returns [7].
Complexity
(j, i) once, about n²/2 checks, which dominates. The rebuild walks at most n parent links. The dp and parent arrays hold n values each.Largest Divisible Subset FAQ
If 1 is in the array, is it always in the answer?
It can always be added. 1 divides every number, so putting it at the start of any chain keeps the subset valid and makes it one longer. The code needs no special case: a longest chain that skipped 1 could be made longer by adding it, so the chain the code finds always starts at 1.
Can the O(n log n) trick for increasing subsequences be used here?
No. That trick keeps one best tail per chain length and binary searches it, which relies on "smaller than" ordering all numbers in one line. "Divides" does not: 4 and 6 are neither divisor nor multiple of each other. So the pair-by-pair O(n²) DP is the standard solution.