Dsa
Graphs Dfs
Medium
Social Network Friend Groups
You are given a number of users in a social network and a list of pairs representing friendships. Your task is to determine how many distinct friend groups exist, where a friend group is defined as a set of users who are connected directly or indirectly through friendships.
Input format:
- The first line contains two integers N (1 <= N <= 1000) and F (0 <= F <= 10000), the number of users and the number of friendships.
- The next F lines each contain two integers representing a friendship between two users (user IDs from 1 to N).
Output format:
- A single integer representing the number of distinct friend groups.
Example:
Input:
5 4
1 2
2 3
4 5
Output:
2
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