Overview
This is a direct application of the Longest Increasing Subsequence (LIS) pattern. By sorting the array first, we guarantee `a < b`, so we only need to check if `b % a == 0`.
Loading...
Loading Curriculum...
Loading Subject...
Loading Topic...
Loading Lesson...
Loading Lab...
Find the largest subset where every pair (a, b) satisfies a % b == 0 or b % a == 0. Uses the LIS pattern with trace-back.
This is a direct application of the Longest Increasing Subsequence (LIS) pattern. By sorting the array first, we guarantee `a < b`, so we only need to check if `b % a == 0`.
`dp[i]` stores the length of the longest divisible subset ending at index `i`. It behaves exactly like LIS.
To reconstruct the actual subset (not just the length), we use a `hash` array. `hash[i]` stores the index of the previous element in the optimal subset.
We find the index with the maximum `dp` value, and trace backwards using `hash[i]` until `hash[i] == i`.