We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Non-Divisible Subset
Non-Divisible Subset
Sort by
recency
|
812 Discussions
|
Please Login in order to post a comment
The fact that the hardest part on this thing was remembering how mods work other than checking if smt is divisible xd
The wording "maximal subset of S" is mistaken. "Maximal" does not mean largest. It means any set that can't be extended by adding a new element of S without violating the definition of the desired subset (the non-divisibility property). The correct term is "maximum", which means largest. Again, "maximal" only means not extendible, and a maximal subset may not be a maximum subset.
Here is my thought process:
Now is the fun part. I first thought it was a DP, but it was much easier. Each pair i+j = k -> choose the one with more elements
with i = 0 and i = k // 2 -> either 1 or 0
o(k/2)
This is a tough problem, not least because the description tacitly leads you to an exhaustive search. As you begin implementing this solution it should be clear this will take forever if the size of the set is not small. One way to reduce the size of the set it to consider the equivalence classes modulo k, instead of the elements themselves. However, this set can still have as many as k elements. The solution needs to be constructive - i.e. you must construct the largest subset. There are undoubtably faster ones, but a readable solution looks like
i%k
; values are arrays containing all elts with that modulo), then from this create a hash table with the same keys, but values are the lengths of the value arrays above.i
, the only eltj
such that(i+j)%k = 0
isk-i
. Thus we need only find which has the larger eq class:i
ork-i
. Find the max and sum them up.k/2
(ifk
is even), and multiples ofk
. Each can have only 1 element in the set. This can be addressed in the hash table containing the lengths of the eq-classes.