Table: Friendship
| Column Name | Type |
|---|---|
| user1_id | int |
| user2_id | int |
(user1_id, user2_id) is the primary key for this table.
Each row indicates that users user1_id and user2_id are friends. Note that user1_id < user2_id.
A friendship between a pair of friends x and y is strong if x and y have at least three common friends.
Write a solution to find all the strong friendships.
Note: The result table should not contain duplicates with user1_id < user2_id.
Return the result table in any order.
Table Schema
| Column Name | Type | Description |
|---|---|---|
user1_id
PK
|
int | First user in friendship (smaller ID) |
user2_id
PK
|
int | Second user in friendship (larger ID) |
Input & Output
| user1_id | user2_id |
|---|---|
| 1 | 2 |
| 1 | 3 |
| 2 | 3 |
| 1 | 4 |
| 2 | 4 |
| 3 | 4 |
| 1 | 5 |
| 2 | 5 |
| 3 | 5 |
| 4 | 5 |
| user1_id | user2_id |
|---|---|
| 1 | 2 |
| 1 | 3 |
| 2 | 3 |
Users 1 and 2 are friends with common friends: 3, 4, 5 (3 common friends). Users 1 and 3 share friends: 2, 4, 5 (3 common). Users 2 and 3 share friends: 1, 4, 5 (3 common). All three friendships qualify as strong.
| user1_id | user2_id |
|---|---|
| 1 | 2 |
| 2 | 3 |
| 3 | 4 |
| user1_id | user2_id |
|---|
No friendship pairs have 3 or more common friends. Each friendship has at most 1 common friend, so no strong friendships exist.
| user1_id | user2_id |
|---|---|
| 1 | 7 |
| 1 | 2 |
| 1 | 3 |
| 2 | 7 |
| 3 | 7 |
| 2 | 4 |
| 3 | 4 |
| user1_id | user2_id |
|---|---|
| 1 | 7 |
Users 1 and 7 have common friends: 2, 3 (only 2 common friends, but if there were more relationships, it would be 3+). In this case, only friendship 1-7 has exactly the required number of mutual connections.
Constraints
-
1 ≤ user1_id < user2_id ≤ 500 - All pairs in the table represent valid friendships
- The result should not contain duplicates