Dsa
Graphs Dfs
Easy
Friends Connection
In a social network, each person is represented as a node and friendships as edges. You need to determine how many distinct friend groups exist in the network. A friend group is defined as a connected component where every person is reachable from every other person in the group.
Input format:
- The first line contains two integers, n (1 ≤ n ≤ 100) for the number of people and m (0 ≤ m ≤ 1000) for the number of friendships.
- The next m lines contain two integers a and b, indicating a friendship between person a and person b.
Output format:
- A single integer representing the number of distinct friend groups.
Example:
Input:
4 3
1 2
2 3
3 4
Output:
1
Key concepts
graphsdfsconnected components
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