Friend triangles
Write code · Medium · §7.2 Permutations and combinations
There are n users on a social network, numbered from 0 to n - 1. A record [a, b] in friends means that a and b are friends with each other, and either number may be written first. Three people who are all friends with one another form a friend triangle.
Complete the function solution, which returns the number of friend triangles. The same three people count only once, whatever order you pick them in.
Function
solution(n, friends)
Constraints
nis an integer from 3 to 90.- The length of
friendsis from 1 ton * (n - 1) // 2. - Each element of
friendshas the form[a, b], whereaandbare different integers from 0 ton - 1. - The same two people appear in at most one record.
Examples
| n | friends | result |
|---|---|---|
4 | [[0, 1], [1, 2], [0, 2], [2, 3]] | 1 |
5 | [[0, 1], [0, 2], [1, 2], [1, 3], [2, 3], [3, 4]] | 2 |
Starter code
def solution(n, friends):
answer = 0
return answerSolve this problem in your browser
Python runs in your browser and checks your answer right away. Hints and the answer are on the problem page.