An equity research desk logs every coverage note as a pair: which analyst wrote it and which stock it covers. Two stocks are said to be linked by an analyst if that analyst has published a coverage note on both stocks. Given the complete list of coverage records, find the largest number of analysts that link any single pair of distinct stocks, then report every pair of stocks achieving this maximum overlap, along with the overlap count.
Let k be the maximum, over all pairs of distinct stock ids appearing in the input, of the number of distinct analyst ids covering both stocks in the pair (only pairs linked by at least one common analyst are considered). Print every pair of distinct stock ids (s1, s2) with s1 < s2 whose common-analyst count equals k, one pair per line, formatted as "s1 s2 k". Order the printed lines by s1 ascending, then by s2 ascending. It is guaranteed that at least one pair of stocks shares at least one common analyst.
Example 1
Input
6 1 10 1 20 1 30 2 10 2 20 3 30
Expected
10 20 2
Explanation
Analyst 1 covers stocks 10, 20 and 30; analyst 2 covers 10 and 20; analyst 3 covers only 30. Stocks 10 and 20 are both covered by analysts 1 and 2, an overlap of 2 -- the highest of any pair (stocks 10 & 30 and 20 & 30 are each only linked by analyst 1, an overlap of 1). So the maximum overlap is k=2, achieved uniquely by the pair (10, 20), printed as '10 20 2'.
Example 2
Input
8 1 5 1 6 2 5 2 6 3 7 3 8 4 7 4 8
Expected
5 6 2 7 8 2
Explanation
Analysts 1 and 2 both cover stocks 5 and 6, giving that pair an overlap of 2. Independently, analysts 3 and 4 both cover stocks 7 and 8, also an overlap of 2. No other pair of stocks shares any common analyst, so the maximum overlap is k=2, tied between (5, 6) and (7, 8); sorted by the first stock id, (5, 6) is printed before (7, 8).
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →