Skip to content

368. Largest Divisible Subset #1525

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to find the largest subset of distinct positive integers such that every pair of elements in the subset satisfies the divisibility condition (i.e., one element divides the other). The approach involves dynamic programming (DP) to efficiently determine the longest chain of elements where each subsequent element is a multiple of the previous one.

Approach

  1. Sort the Array: Sorting helps in ensuring that if we are considering elements in ascending order, each subsequent element can only be a multiple of the previous elements.
  2. Dynamic Programming Setup: Use two arrays:
    • dp[i] to store the length of the largest divisible subset ending with the element at index i.
    • prev[i] to track the in…

Replies: 1 comment 2 replies

Comment options

mah-shamim
Apr 6, 2025
Maintainer Author

You must be logged in to vote
2 replies
@basharul-siddike
Comment options

@mah-shamim
Comment options

mah-shamim Apr 6, 2025
Maintainer Author

Answer selected by basharul-siddike
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants