368. Largest Divisible Subset - Detailed Explanation
Problem Statement
Given a set of distinct positive integers nums
, return the largest subset such that for every pair (Si, Sj)
in the subset, either Si % Sj == 0
or Sj % Si == 0
. If there are multiple solutions, return any.
Examples
Example 1
Input nums = [1,2,3]
Output [1,2] (or [1,3])
Example 2
Input nums = [1,2,4,8]
Output [1,2,4,8]
Constraints
- 1 ≤ nums.length ≤ 1000
- 1 ≤ nums[i] ≤ 10⁹
- All values of nums are distinct
Intuition
If you sort nums
, any valid divisible pair in the subset must respect the sorted order. You can build up the solution by considering each number as the largest in a subset ending there, and keeping track of the best chain you can attach to it.
Dynamic Programming Formulation
- Sort
nums
in ascending order. - Let
dp[i]
= size of the largest divisible subset ending at index i. - Let
prev[i]
= index of the previous element in that subset (or -1 if none). - Initialize each
dp[i] = 1
,prev[i] = -1
. - For each
i
from 0 to n−1:- For each
j
from 0 to i−1:- If
nums[i] % nums[j] == 0
anddp[j] + 1 > dp[i]
:- Update
dp[i] = dp[j] + 1
- Set
prev[i] = j
- Update
- If
- For each
- Track the index
maxIdx
wheredp
is largest. - Reconstruct the subset by following
prev
frommaxIdx
back to -1.
Step‑by‑Step Walkthrough
nums = [1,2,4,7,8]
- Sort (already sorted).
- Initialize
dp = [1,1,1,1,1] prev = [-1,-1,-1,-1,-1]
- i=1 (num=2):
- j=0 (1): 2%1==0, dp[0]+1=2>1 → dp[1]=2, prev[1]=0
- i=2 (num=4):
- j=0: 4%1==0 → dp[2]=2, prev[2]=0
- j=1: 4%2==0 and dp[1]+1=3>2 → dp[2]=3, prev[2]=1
- i=3 (num=7):
- only j where divisible is none → dp[3]=1
- i=4 (num=8):
- j=0: 8%1==0 → dp[4]=2, prev[4]=0
- j=1: 8%2==0 → dp[4]=3, prev[4]=1
- j=2: 8%4==0 → dp[4]=4, prev[4]=2
- j=3: skip
- dp = [1,2,3,1,4], maxIdx = 4
- Reconstruct: 8 ← prev[4]=2 → 4 ← prev[2]=1 → 2 ← prev[1]=0 → 1
→ subset = [1,2,4,8]
Complexity Analysis
- Time: O(n²) due to the double loop over i and j
- Space: O(n) for
dp
,prev
, and the output list
Common Mistakes
- Forgetting to sort before DP
- Not initializing
prev
to -1 and handling reconstruction correctly - Off‑by‑one when building the output in reverse order
Edge Cases
- Single element → return
[that element]
- All primes (no divisible pairs) → return any single element
- Already fully divisible chain → returns the entire sorted list
Code Solutions
Python
Python3
Python3
. . . .
Java
Java
Java
. . . .
Related Problems
TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-
GET YOUR FREE
Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
4.6
(69,299 learners)
New
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
3.9
(1,107 learners)
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
4
(26,683 learners)
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.