Given a Friends table that represents friendship relationships, find all pairs of users who are friends with each other but have no mutual friends.
The Friends table contains pairs of user IDs where each row indicates that user_id1 and user_id2 are friends with each other. The friendship relationship is bidirectional.
Return the result table ordered by user_id1, user_id2 in ascending order.
Table Schema
| Column Name | Type | Description |
|---|---|---|
user_id1
PK
|
int | First user in the friendship pair |
user_id2
PK
|
int | Second user in the friendship pair |
Input & Output
| user_id1 | user_id2 |
|---|---|
| 1 | 2 |
| 1 | 3 |
| 2 | 3 |
| 4 | 5 |
| user_id1 | user_id2 |
|---|---|
| 4 | 5 |
Users 1, 2, and 3 form a triangle where each pair has a mutual friend: (1,2) share friend 3, (1,3) share friend 2, and (2,3) share friend 1. Only users 4 and 5 are friends with no mutual connections.
| user_id1 | user_id2 |
|---|---|
| 1 | 2 |
| 3 | 4 |
| 5 | 6 |
| user_id1 | user_id2 |
|---|---|
| 1 | 2 |
| 3 | 4 |
| 5 | 6 |
All friendship pairs are isolated - none of them share mutual friends, so all pairs are returned in the result.
| user_id1 | user_id2 |
|---|---|
| 1 | 2 |
| 1 | 3 |
| 2 | 3 |
| 1 | 4 |
| 2 | 4 |
| 3 | 4 |
| user_id1 | user_id2 |
|---|
This forms a complete graph where every pair of users has mutual friends. For example, (1,2) share friends 3 and 4, so no pairs qualify as having no mutual friends.
Constraints
-
1 ≤ user_id1, user_id2 ≤ 10^6 -
user_id1 ≠ user_id2 - All friendship relationships are bidirectional