Grouping Algorithm

It is necessary to develop an algorithm to solve the following problem

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) 
+5
source share
1 answer

Given the N sets (marked with Ni ) of integers sorted , initialize the N variables Hi that will hold the heads of each set.

While there are still indexes Hi that have not reached the end of their corresponding Ni , iterate over the values Vi=Ni[Hi] and find the minimum value Vmin , count the number of occurrences n and save the corresponding indexes j (which you can do in one cycle).

The increment Hj .

If n>X , this will give you a new set M = [Vmin] (from Nj) .

It is up to you to model the presentation of the data accordingly to use (from Nj) as the map key.

+1
source

Source: https://habr.com/ru/post/1233393/


All Articles