368. Largest Divisible Subset LeetCode Solution
In this guide, you will get 368. Largest Divisible Subset LeetCode Solution with the best time and space complexity. The solution to Largest Divisible Subset problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Largest Divisible Subset solution in C++
- Largest Divisible Subset solution in Java
- Largest Divisible Subset solution in Python
- Additional Resources
Problem Statement of Largest Divisible Subset
Given a set of distinct positive integers nums, return the largest subset answer such that every pair (answer[i], answer[j]) of elements in this subset satisfies:
answer[i] % answer[j] == 0, or
answer[j] % answer[i] == 0
If there are multiple solutions, return any of them.
Example 1:
Input: nums = [1,2,3]
Output: [1,2]
Explanation: [1,3] is also accepted.
Example 2:
Input: nums = [1,2,4,8]
Output: [1,2,4,8]
Constraints:
1 <= nums.length <= 1000
1 <= nums[i] <= 2 * 109
All the integers in nums are unique.
Complexity Analysis
- Time Complexity: O(n^2)
- Space Complexity: O(n)
368. Largest Divisible Subset LeetCode Solution in C++
class Solution {
public:
vector<int> largestDivisibleSubset(vector<int>& nums) {
const int n = nums.size();
vector<int> ans;
// sizeEndsAt[i] := the maximum size ends in nums[i]
vector<int> sizeEndsAt(n, 1);
// prevIndex[i] := the best index s.t.
// 1. nums[i] % nums[prevIndex[i]] == 0 and
// 2. can increase the size of the subset
vector<int> prevIndex(n, -1);
int maxSize = 0; // Max size of the subset
int index = -1; // Track the best ending index
ranges::sort(nums);
// Fix the maximum ending number in the subset.
for (int i = 0; i < n; ++i) {
for (int j = i - 1; j >= 0; --j)
if (nums[i] % nums[j] == 0 && sizeEndsAt[i] < sizeEndsAt[j] + 1) {
sizeEndsAt[i] = sizeEndsAt[j] + 1;
prevIndex[i] = j;
}
// Find a new subset that has a bigger size.
if (maxSize < sizeEndsAt[i]) {
maxSize = sizeEndsAt[i];
index = i; // Update the best ending index.
}
}
// Loop from the back to the front.
while (index != -1) {
ans.push_back(nums[index]);
index = prevIndex[index];
}
return ans;
}
};
/* code provided by PROGIEZ */
368. Largest Divisible Subset LeetCode Solution in Java
class Solution {
public List<Integer> largestDivisibleSubset(int[] nums) {
final int n = nums.length;
List<Integer> ans = new ArrayList<>();
// sizeEndsAt[i] := the maximum size ends in nums[i]
int[] sizeEndsAt = new int[n];
// prevIndex[i] := the best index s.t.
// 1. nums[i] % nums[prevIndex[i]] == 0 and
// 2. can increase the size of the subset
int[] prevIndex = new int[n];
int maxSize = 0; // Max size of the subset
int index = -1; // Track the best ending index
Arrays.fill(sizeEndsAt, 1);
Arrays.fill(prevIndex, -1);
Arrays.sort(nums);
// Fix the maximum ending number in the subset.
for (int i = 0; i < n; ++i) {
for (int j = i - 1; j >= 0; --j)
if (nums[i] % nums[j] == 0 && sizeEndsAt[i] < sizeEndsAt[j] + 1) {
sizeEndsAt[i] = sizeEndsAt[j] + 1;
prevIndex[i] = j;
}
// Find a new subset that has a bigger size.
if (maxSize < sizeEndsAt[i]) {
maxSize = sizeEndsAt[i];
index = i; // Update the best ending index.
}
}
// Loop from the back to the front.
while (index != -1) {
ans.add(nums[index]);
index = prevIndex[index];
}
return ans;
}
}
// code provided by PROGIEZ
368. Largest Divisible Subset LeetCode Solution in Python
class Solution:
def largestDivisibleSubset(self, nums: list[int]) -> list[int]:
n = len(nums)
ans = []
count = [1] * n
prevIndex = [-1] * n
maxCount = 0
index = -1
nums.sort()
for i, num in enumerate(nums):
for j in reversed(range(i)):
if num % nums[j] == 0 and count[i] < count[j] + 1:
count[i] = count[j] + 1
prevIndex[i] = j
if count[i] > maxCount:
maxCount = count[i]
index = i
while index != -1:
ans.append(nums[index])
index = prevIndex[index]
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.