Easy explanation C++ | JAVA | PYTHON | JAVASCRIPT
Intuition\nTo find the largest divisible subset, we need to consider all pairs of numbers in the given set. We can use dynamic programming to keep track of the 
# Intuition\nTo find the largest divisible subset, we need to consider all pairs of numbers in the given set. We can use dynamic programming to keep track of the length of the divisible subset ending at each index.\n\n# Approach\n1) Sort the input array nums in ascending order.\n\n2) Create two arrays, dp and prev, both of size n (where n is the size of nums).\n\n3) Initialize all elements of dp to 1 and prev to -1.\n\n4) Iterate through the sorted nums array and update dp and prev arrays.\n\nFor each pair (nums[i], nums[j]), if nums[i] % nums[j] == 0 and dp[i] < dp[j] + 1, update dp[i] and prev[i].\n\n5) Find the index of the maximum value in the dp array.\n\n6) Reconstruct the largest divisible subset using the prev array.\n\n7) Return the result.\n\n# Complexity\n- Time complexity:\nO(n^2) (due to nested loops)\n\n- Space complexity:\nO(n) (for the dp and prev arrays)\n\n``` c++ []\n#include <vector>\n#include <algorithm>\n\nclass Solution {\npublic:\n    vector<int> largestDivisibleSubset(vector<int>& nums) {\n        int n = nums.size();\n        if (n <= 1) {\n            return nums;\n        }\n\n        sort(nums.begin(), nums.end());\n\n        vector<int> dp(n, 1);\n        vector<int> prev(n, -1);\n\n        int maxIndex = 0;\n\n        for (int i = 1; i < n; ++i) {\n            for (int j = 0; j < i; ++j) {\n                if (nums[i] % nums[j] == 0 && dp[i] < dp[j] + 1) {\n                    dp[i] = dp[j] + 1;\n                    prev[i] = j;\n                }\n            }\n\n            if (dp[i] > dp[maxIndex]) {\n                maxIndex = i;\n            }\n        }\n\n        vector<int> result;\n        while (maxIndex != -1) {\n            result.push_back(nums[maxIndex]);\n            maxIndex = prev[maxIndex];\n        }\n\n        reverse(result.begin(), result.end());\n        return result;\n    }\n};\n```\n```javascript []\n/**\n * @param {number[]} nums\n * @return {number[]}\n */\nvar largestDivisibleSubset = function(nums) {\n    const n = nums.length;\n    if (n <= 1) {\n        return nums;\n    }\n\n    nums.sort((a, b) => a - b);\n    const dp = new Array(n).fill(1);\n    const prev = new Array(n).fill(-1);\n    let maxIndex = 0;\n\n    for (let i = 1; i < n; ++i) {\n        for (let j = 0; j < i; ++j) {\n            if (nums[i] % nums[j] === 0 && dp[i] < dp[j] + 1) {\n                dp[i] = dp[j] + 1;\n                prev[i] = j;\n            }\n        }\n\n        if (dp[i] > dp[maxIndex]) {\n            maxIndex = i;\n        }\n    }\n\n    const result = [];\n    while (maxIndex !== -1) {\n        result.push(nums[maxIndex]);\n        maxIndex = prev[maxIndex];\n    }\n\n    return result.reverse();\n};\n\n```\n```python []\nclass Solution:\n    def largestDivisibleSubset(self, nums):\n        n = len(nums)\n        if n <= 1:\n            return nums\n\n        nums.sort()\n        dp = [1] * n\n        prev = [-1] * n\n        maxIndex = 0\n\n        for i in range(1, n):\n            for j in range(i):\n                if nums[i] % nums[j] == 0 and dp[i] < dp[j] + 1:\n                    dp[i] = dp[j] + 1\n                    prev[i] = j\n\n            if dp[i] > dp[maxIndex]:\n                maxIndex = i\n\n        result = []\n        while maxIndex != -1:\n            result.append(nums[maxIndex])\n            maxIndex = prev[maxIndex]\n\n        return result[::-1]\n\n```\n```java []\nimport java.util.Arrays;\nimport java.util.ArrayList;\nimport java.util.List;\n\nclass Solution {\n    public List<Integer> largestDivisibleSubset(int[] nums) {\n        int n = nums.length;\n        if (n <= 1) {\n            return Arrays.asList(nums);\n        }\n\n        Arrays.sort(nums);\n        int[] dp = new int[n];\n        int[] prev = new int[n];\n        Arrays.fill(dp, 1);\n        Arrays.fill(prev, -1);\n        int maxIndex = 0;\n\n        for (int i = 1; i < n; ++i) {\n            for (int j = 0; j < i; ++j) {\n                if (nums[i] % nums[j] == 0 && dp[i] < dp[j] + 1) {\n                    dp[i] = dp[j] + 1;\n                    prev[i] = j;\n                }\n            }\n\n            if (dp[i] > dp[maxIndex]) {\n                maxIndex = i;\n            }\n        }\n\n        List<Integer> result = new ArrayList<>();\n        while (maxIndex != -1) {\n            result.add(nums[maxIndex]);\n            maxIndex = prev[maxIndex];\n        }\n\n        java.util.Collections.reverse(result);\n        return result;\n    }\n}\n\n```\n\nUPVOTE IF YOU FIND ITS USEFULL\n\n```
['Python', 'C++', 'Java', 'JavaScript']