**Friend Groups - Amazon Top Interview Questions**

### Problem Statement :

You are given an undirected graph friends as an adjacency list, where friends[i] is a list of people i is friends with. Friendships are two-way. Two people are in a friend group as long as there is some path of mutual friends connecting them. Return the total number of friend groups. Constraints n ≤ 250 where n is the length of friends Example 1 Input friends = [ [1], [0, 2], [1], [4], [3], [] ] Output 3 Explanation The three friend groups are [0, 1, 2] [3, 4] [5]

### Solution :

` ````
Solution in C++ :
int find(int x, vector<int>& parent) {
if (parent[x] == -1) return x;
return find(parent[x], parent);
}
void unionn(int x, int y, vector<int>& parent) {
int a = find(x, parent);
// cout<<a<<" ";
int b = find(y, parent);
// cout<<b<<endl;
if (a == b) return;
parent[b] = a;
}
int solve(vector<vector<int>>& f) {
if (f.empty()) return 0;
int maxx = INT_MIN;
for (int i = 0; i < f.size(); i++) {
for (int j = 0; j < f[i].size(); j++) {
maxx = max(f[i][j], maxx);
}
}
vector<int> p(maxx + 1, -1);
vector<int> v;
for (int i = 0; i < f.size(); i++) {
if (f[i].size() == 0 || f.size() == 1) continue;
for (int j = 1; j < f[i].size(); j++) {
// cout<<f[i][0]<<" "<<f[i][j]<<endl;
unionn(f[i][0], f[i][j], p);
}
}
int count = 0;
for (int i = 0; i < p.size(); i++) {
if (p[i] == -1) count++;
// cout<<p[i]<<" ";
}
return count;
}
```

` ````
Solution in Python :
class DSU:
def __init__(self, n):
self.parent = [i for i in range(n)]
self.rank = [0 for i in range(n)]
def find(self, x):
if self.parent[x] == x:
return self.parent[x]
self.parent[x] = self.find(self.parent[x])
return self.parent[x]
def union(self, u, v):
pu = self.find(u)
pv = self.find(v)
if self.rank[pu] < self.rank[v]:
pu, pv = pv, pu
self.parent[pv] = pu
self.rank[pu] += self.rank[pv]
def count(self):
par_set = set()
for parent in self.parent:
par_set.add(self.find(parent))
return len(par_set)
class Solution:
def solve(self, A):
n = len(A)
dsu = DSU(n)
for user, friends in enumerate(A):
for friend in friends:
dsu.union(user, friend)
return dsu.count()
```

## View More Similar Problems

## Print the Elements of a Linked List

This is an to practice traversing a linked list. Given a pointer to the head node of a linked list, print each node's data element, one per line. If the head pointer is null (indicating the list is empty), there is nothing to print. Function Description: Complete the printLinkedList function in the editor below. printLinkedList has the following parameter(s): 1.SinglyLinkedListNode

View Solution →## Insert a Node at the Tail of a Linked List

You are given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer. Insert this node at the tail of the linked list and return the head node of the linked list formed after inserting this new node. The given head pointer may be null, meaning that the initial list is empty. Input Format: You have to complete the SinglyLink

View Solution →## Insert a Node at the head of a Linked List

Given a pointer to the head of a linked list, insert a new node before the head. The next value in the new node should point to head and the data value should be replaced with a given value. Return a reference to the new head of the list. The head pointer given may be null meaning that the initial list is empty. Function Description: Complete the function insertNodeAtHead in the editor below

View Solution →## Insert a node at a specific position in a linked list

Given the pointer to the head node of a linked list and an integer to insert at a certain position, create a new node with the given integer as its data attribute, insert this node at the desired position and return the head node. A position of 0 indicates head, a position of 1 indicates one node away from the head and so on. The head pointer given may be null meaning that the initial list is e

View Solution →## Delete a Node

Delete the node at a given position in a linked list and return a reference to the head node. The head is at position 0. The list may be empty after you delete the node. In that case, return a null value. Example: list=0->1->2->3 position=2 After removing the node at position 2, list'= 0->1->-3. Function Description: Complete the deleteNode function in the editor below. deleteNo

View Solution →## Print in Reverse

Given a pointer to the head of a singly-linked list, print each data value from the reversed list. If the given list is empty, do not print anything. Example head* refers to the linked list with data values 1->2->3->Null Print the following: 3 2 1 Function Description: Complete the reversePrint function in the editor below. reversePrint has the following parameters: Sing

View Solution →