C++ || DP || 36 ms, faster than 94.40%
\nclass Solution {\npublic:\n    vector<int> largestDivisibleSubset(vector<int>& nums) {\n        int n = nums.size();\n//sort the given vector to ensure that a
```\nclass Solution {\npublic:\n    vector<int> largestDivisibleSubset(vector<int>& nums) {\n        int n = nums.size();\n//sort the given vector to ensure that all factors comes before a number in the given vector\n        sort(nums.begin(), nums.end()); \n        vector<int> lis(n,1);//lis store the maximum length of subset such that given constrain hold\n// we traverse for all position and check what is the longest length till index i holding the constrain\n//NOTE: if(nums[i]%nums[j]==0 and nums[i]%nums[k]==0 then nums[j]%nums[k] ==0), this property is being used here\n        for(int i=1;i<n;i++)\n            for(int j=0;j<i;j++){\n                if(nums[i]%nums[j]==0)\n                    lis[i]= max(lis[i], 1+lis[j]);\n            }\n        int max_index=0; //variable to store the index of maximum value in lis vector\n// Now traverse the lis vector to find index which has the maximum value \n        for(int i=0;i<n;i++){\n            if(lis[i]>lis[max_index])\n                max_index = i;\n        }\n        vector<int> ans; //vector to store the result\n        int temp_val = nums[max_index]; \n        int count = lis[max_index];\n //now we traverse from max_index obtained to index 0 and check if the constrain hold if yes push it to ans vector   \n        for(int i= max_index;i>=0;){\n            if(temp_val%nums[i]==0 && count==lis[i]){\n                ans.push_back(nums[i]);\n                temp_val = nums[i];\n                count--;\n            }\n            i--;\n        }\n        return ans;\n    }\n};\n```\n**Do upvote explanation was useful and you liked the code :)**
[]