Need to develop an algorithm to solve the following task
Given:
The N sets with a different number of elements
Expected Result:
The new M sets containing ≥X common elements of the N sets
Example:
N1=[1,2,3,4,5]
N2=[2,3,5]
N3=[1,3,5]
N4=[1,2]
if X=3:
M1=[1] (from N1,3,4)
M2=[2] (from N1,2,4)
M3=[3,5] (from N1,2,3)
Given N sets (noted Ni
) of sorted integers, initialize N variablesHi
which will hold the heads of each set.
While there still exist indexesHi
that haven't reached the end of their respectiveNi
, iterate over the valuesVi=Ni[Hi]
and find the minimum valueVmin
, count the number of occurrencesn
and store the corresponding indexesj
(which you can all do in one loop).
Increment theHj
.
Ifn>X
, this gives you a new setM = [Vmin] (from Nj)
.
Up to you to model the data representation accordingly so as to use(from Nj)
as a map key.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With