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
numsin 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
ifrom 0 to n−1:- For each
jfrom 0 to i−1:- If
nums[i] % nums[j] == 0anddp[j] + 1 > dp[i]:- Update
dp[i] = dp[j] + 1 - Set
prev[i] = j
- Update
- If
- For each
- Track the index
maxIdxwheredpis largest. - Reconstruct the subset by following
prevfrommaxIdxback 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
prevto -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
2290. Minimum Obstacle Removal to Reach Corner - Detailed Explanation
Learn to solve Leetcode 2290. Minimum Obstacle Removal to Reach Corner with multiple approaches.
3264. Final Array State After K Multiplication Operations I - Detailed Explanation
Learn to solve Leetcode 3264. Final Array State After K Multiplication Operations I with multiple approaches.
17. Letter Combinations of a Phone Number - Detailed Explanation
Learn to solve Leetcode 17. Letter Combinations of a Phone Number with multiple approaches.
91. Decode Ways - Detailed Explanation
Learn to solve Leetcode 91. Decode Ways with multiple approaches.
378. Kth Smallest Element in a Sorted Matrix - Detailed Explanation
Learn to solve Leetcode 378. Kth Smallest Element in a Sorted Matrix with multiple approaches.
1800. Maximum Ascending Subarray Sum - Detailed Explanation
Learn to solve Leetcode 1800. Maximum Ascending Subarray Sum with multiple approaches.
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
$197

Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
3.9
$78

Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
4
$78
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.