C++ Recursive to Memoization
Recursion Code: This will give TLE.\n\nclass Solution {\npublic:\n    vector<int> ans;\n    void lds(vector<int> temp,int i,int prev,vector<int>& nums){\n      
Recursion Code: This will give TLE.\n```\nclass Solution {\npublic:\n    vector<int> ans;\n    void lds(vector<int> temp,int i,int prev,vector<int>& nums){\n        \n        if(i>=nums.size()){\n            if(temp.size()>ans.size())  ans=temp;\n            return;\n        }\n        if(nums[i]%prev==0){  \n            temp.push_back(nums[i]);\n            lds(temp,i+1,nums[i],nums);\n            temp.pop_back();\n        }\n        lds(temp,i+1,prev,nums);\n    }\n    \n    vector<int> largestDivisibleSubset(vector<int>& nums) {\n        sort(nums.begin(),nums.end());\n        vector<int> temp;\n        lds(temp,0,1,nums);\n        return ans;\n    }\n};\n```\n\nFrom Recursion to Memoziation : Accepted\n\n```\nclass Solution {\npublic:\n    vector<int> ans;\n    vector<int> dp;\n    void lds(vector<int> temp,int i,int prev,vector<int>& nums){\n        if(i>=nums.size()){\n            if(temp.size()>ans.size()) ans=temp;\n            return;\n        }\n        //We can\'t directly use temp.size() without typecasting because it will return an unsigned int and hence if() will not work.\n        if((int)temp.size()>dp[i] && (nums[i]%prev==0)){ \n            dp[i]=temp.size();\n            temp.push_back(nums[i]);\n            lds(temp,i+1,nums[i],nums);\n            temp.pop_back();\n        }\n        lds(temp,i+1,prev,nums);\n    }\n    \n    vector<int> largestDivisibleSubset(vector<int>& nums) {\n        sort(nums.begin(),nums.end());\n        for(int i=0;i<=nums.size();i++) dp.push_back(-1);\n        vector<int> temp;\n        lds(temp,0,1,nums);\n        return ans;\n    }\n};\n```
['Recursion', 'Memoization', 'C']