Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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:
answer[i] % answer[j] == 0, oranswer[j] % answer[i] == 0If there are multiple solutions, return any of them.
Example 1:
Input: nums = [1,2,3] Output: [1,2] Explanation: [1,3] is also accepted.
Example 2:
Input: nums = [1,2,4,8] Output: [1,2,4,8]
Constraints:
1 <= nums.length <= 10001 <= nums[i] <= 2 * 109nums are unique.Problem summary: 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: answer[i] % answer[j] == 0, or answer[j] % answer[i] == 0 If there are multiple solutions, return any of them.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math · Dynamic Programming
[1,2,3]
[1,2,4,8]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #368: Largest Divisible Subset
class Solution {
public List<Integer> largestDivisibleSubset(int[] nums) {
Arrays.sort(nums);
int n = nums.length;
int[] f = new int[n];
Arrays.fill(f, 1);
int k = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < i; ++j) {
if (nums[i] % nums[j] == 0) {
f[i] = Math.max(f[i], f[j] + 1);
}
}
if (f[k] < f[i]) {
k = i;
}
}
int m = f[k];
List<Integer> ans = new ArrayList<>();
for (int i = k; m > 0; --i) {
if (nums[k] % nums[i] == 0 && f[i] == m) {
ans.add(nums[i]);
k = i;
--m;
}
}
return ans;
}
}
// Accepted solution for LeetCode #368: Largest Divisible Subset
func largestDivisibleSubset(nums []int) (ans []int) {
sort.Ints(nums)
n := len(nums)
f := make([]int, n)
k := 0
for i := 0; i < n; i++ {
f[i] = 1
for j := 0; j < i; j++ {
if nums[i]%nums[j] == 0 {
f[i] = max(f[i], f[j]+1)
}
}
if f[k] < f[i] {
k = i
}
}
m := f[k]
for i := k; m > 0; i-- {
if nums[k]%nums[i] == 0 && f[i] == m {
ans = append(ans, nums[i])
k = i
m--
}
}
return
}
# Accepted solution for LeetCode #368: Largest Divisible Subset
class Solution:
def largestDivisibleSubset(self, nums: List[int]) -> List[int]:
nums.sort()
n = len(nums)
f = [1] * n
k = 0
for i in range(n):
for j in range(i):
if nums[i] % nums[j] == 0:
f[i] = max(f[i], f[j] + 1)
if f[k] < f[i]:
k = i
m = f[k]
i = k
ans = []
while m:
if nums[k] % nums[i] == 0 and f[i] == m:
ans.append(nums[i])
k, m = i, m - 1
i -= 1
return ans
// Accepted solution for LeetCode #368: Largest Divisible Subset
impl Solution {
pub fn largest_divisible_subset(nums: Vec<i32>) -> Vec<i32> {
let mut nums = nums;
nums.sort();
let n = nums.len();
let mut f = vec![1; n];
let mut k = 0;
for i in 0..n {
for j in 0..i {
if nums[i] % nums[j] == 0 {
f[i] = f[i].max(f[j] + 1);
}
}
if f[k] < f[i] {
k = i;
}
}
let mut m = f[k];
let mut ans = Vec::new();
for i in (0..=k).rev() {
if nums[k] % nums[i] == 0 && f[i] == m {
ans.push(nums[i]);
k = i;
m -= 1;
}
}
ans
}
}
// Accepted solution for LeetCode #368: Largest Divisible Subset
function largestDivisibleSubset(nums: number[]): number[] {
nums.sort((a, b) => a - b);
const n = nums.length;
const f: number[] = Array(n).fill(1);
let k = 0;
for (let i = 0; i < n; ++i) {
for (let j = 0; j < i; ++j) {
if (nums[i] % nums[j] === 0) {
f[i] = Math.max(f[i], f[j] + 1);
}
}
if (f[k] < f[i]) {
k = i;
}
}
let m = f[k];
const ans: number[] = [];
for (let i = k; m > 0; --i) {
if (nums[k] % nums[i] === 0 && f[i] === m) {
ans.push(nums[i]);
k = i;
--m;
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.