Dsa
Union Find
Easy
Merge Employees
You have a list of employee pairs who are considered to be in the same team. Given a list of these pairs, determine the number of distinct teams formed by merging. Each employee is represented by a unique ID (0-indexed). Your task is to return the number of distinct teams after processing all pairs.
Input format:
- The first line contains an integer n, the number of employees.
- The next line contains an integer m, the number of pairs.
- The subsequent m lines each contain two integers a and b, representing that employees a and b are in the same team.
Output format:
- A single integer representing the number of distinct teams.
Example:
Input:
5
4
0 1
1 2
3 4
0 3
Output:
2
Key concepts
union_finddynamic_groupscompany_structures
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