A regional cooking competition keeps a running log of every dish prepared during the season. Each log entry records which head chef led the dish and which sous chef assisted them. Two people form a "seasoned duo" if they have worked together in that exact head-chef/sous-chef arrangement on at least 3 different dishes over the season. Given the full log, list every seasoned duo.
Line 1: a single integer n, the number of dish entries in the log.
Next n lines: two integers head_chef_id and sous_chef_id -- the ids of the head chef and sous chef for that dish. The same ordered pair of ids may repeat across multiple entries (once per dish they cooked together), and a chef's id may appear as a head chef in one entry and as a sous chef in another.
First print a single integer k, the number of seasoned duos. Then print k lines, each containing head_chef_id sous_chef_id for one seasoned duo, sorted ascending primarily by head_chef_id and secondarily by sous_chef_id. If no duo qualifies, print 0 and nothing further.
1 <= n <= 100001 <= head_chef_id, sous_chef_id <= 100000head_chef_id and sous_chef_id may be equal within a single entry(head_chef_id, sous_chef_id) is an ordered pair: a duo with ids swapped is a different duoExample 1
Input
8 1 1 1 1 1 1 1 2 1 2 2 1 2 1 2 2
Expected
1 1 1
Explanation
Head chef 1 and sous chef 1 appear together on the first three entries, meeting the threshold of 3. Head chef 1 with sous chef 2 shares only 2 dishes, and head chef 2 with sous chef 1 or sous chef 2 each share fewer than 3 dishes, so (1,1) is the only seasoned duo.
Example 2
Input
7 2 3 2 3 2 3 2 3 1 5 1 5 1 5
Expected
2 1 5 2 3
Explanation
Head chef 2 and sous chef 3 cooked together 4 times, and head chef 1 and sous chef 5 cooked together 3 times -- both meet the threshold. Sorted ascending by head_chef_id, (1,5) is printed before (2,3), giving a count of 2.
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 →