Skip to content
BytePatterns

Mutual Follow Pairs

EasyGraphs#adjacency-set#directed-graph~15m

Problem

A social app stores who follows whom as a list of pairs [a, b], meaning person a follows person b, with people numbered 0 to n - 1. The list may contain the same pair more than once, and nobody follows themselves. Return how many unordered pairs of people follow each other.

Examples

Input:  n = 4, follows = [[0, 1], [1, 0], [1, 2], [2, 3], [3, 2], [0, 2]]
Output: 2
Why:    0 and 1, and 2 and 3; 1 follows 2 but not the other way round
Input:  n = 3, follows = [[0, 1], [0, 1], [1, 0]]
Output: 1
Why:    the repeated pair still describes one mutual pair
Input:  n = 2, follows = []
Output: 0
Why:    edge case, nobody follows anyone

Hints

0 / 3

Stuck on the idea rather than the code? List vs Matrix covers it.