Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

largest-divisibel-sebset.cpp added #135

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions Regular-Question-Answers/largest-divisible-subset.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Author: Racha Badreddine
// Reviewer:
// Question Link: https://leetcode.com/problems/largest-divisible-subset/

// Time Complexity Analysis: O(n^2)
// Sorting has O(n log n) complexity but is neglected
class Solution {
public:
vector<int> largestDivisibleSubset(vector<int>& nums) {
// Sort the array
sort(nums.begin(), nums.end());

// Variable to store the array size
int size = nums.size();

// Vector to hold the largest divisible subsets starting at each index i
vector<vector<int>> dp(size);

// Vector to hold the largest divisible subset
vector<int> result;

// Iterate through elements starting from the end
for(int i = size - 1; i >= 0; i--) {

// The smallest divisible subset contains the element itself
dp[i].push_back(nums[i]);

// Iterate through the elements larger than the current one
for(int j = i + 1; j < size; j++) {

// If the element is divisible AND if adding the current element to the subset already stored in the jth element is larger than our current subset
if(nums[j] % nums[i] == 0 && (dp[j].size() + 1 > dp[i].size())) {
// Copy the subset to the current element
dp[i] = dp[j];
// Insert the ith element to the beginning of the subset
dp[i].insert(dp[i].begin(), nums[i]);
}
}

// If the current subset is larger than the previous one, copy it to the result
if(dp[i].size() > result.size()) result = dp[i];
}

// Return the largest divisible subset
return result;
}
};