Dsa
Union Find
Medium
Collaborative Filtering Groups
In a collaborative filtering recommendation system, users can recommend products to each other. Based on a series of recommendations, your task is to identify how many distinct groups of users can be formed. If a user recommends a product to another user, they belong to the same group. Your program should find the total number of distinct user groups based on the recommendations given.
Input Format:
- The first line contains an integer R, the number of recommendations (1 <= R <= 10000).
- The next R lines contain two integers: User_A and User_B (1 <= User_A, User_B <= 1000), indicating a recommendation from User_A to User_B.
Output Format:
- Print a single integer: the number of distinct user groups based on the recommendations.
Example:
Input:
4
1 2
2 3
3 4
5 6
Output:
2
Note:
- Group 1: {1, 2, 3, 4}
- Group 2: {5, 6}
Key concepts
union_findrecommendation_systemuser_groups
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now