An online art gallery keeps a log of every time someone opens a painting's detail page. Each entry in the log records the painting that was opened, the id of the artist who created it, the id of the visitor who opened it, and the day (as a day number) it happened. An artist is said to have viewed their own work if, on some log entry, the visitor id matches the id of the artist who created the painting being viewed. Find every artist who has viewed their own work at least once, and report their ids in ascending order.
Line 1: an integer N — the number of log entries. Each of the next N lines contains four integers: painting_id, artist_id, viewer_id, day — describing one log entry.
Print the distinct artist ids who viewed their own work at least once, sorted in ascending order, space-separated on a single line. If no artist qualifies, print an empty line.
Example 1
Input
4 101 5 5 10 102 7 3 11 103 5 8 12 104 3 3 13
Expected
3 5
Explanation
Row 1 (painting 101, artist 5, viewer 5) is a self-view, so artist 5 qualifies. Row 2 (artist 7, viewer 3) is not a self-view. Row 3 (artist 5, viewer 8) is not a self-view, but artist 5 already qualified from row 1. Row 4 (artist 3, viewer 3) is a self-view, so artist 3 also qualifies. The qualifying artists, sorted ascending, are 3 and 5.
Example 2
Input
3 1 10 20 1 2 20 30 2 3 30 10 3
Expected
(empty)Explanation
In every row the viewer id differs from that row's artist id (10 vs 20, 20 vs 30, 30 vs 10), so no artist ever viewed their own painting. The output is an empty line, even though the ids 10, 20, and 30 each appear as both an artist and a viewer somewhere in the log.
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 →