A supply convoy travels as a single line of wagons, each carrying one cargo id. A safety office maintains a flagged list of cargo ids that are no longer permitted in the convoy; if a cargo id appears anywhere on the flagged list (the list may itself repeat an id), every wagon carrying that id must be pulled, no matter where it sits in the line or how many wagons share that id. After the pull, the surviving wagons are re-coupled in their original left-to-right order, with no gaps. Report the cargo ids of the surviving wagons, in order.
The first line contains an integer n, the number of wagons in the convoy. The second line contains n integers, the cargo ids of the wagons in order. The third line contains an integer m, the number of entries on the flagged list. The fourth line (present even when m = 0, in which case it is empty) contains m integers, the flagged cargo ids.
Print one line containing the cargo ids of the surviving wagons, in their original order, separated by single spaces. If no wagons survive, print an empty line.
Example 1
Input
6 9 14 6 14 20 6 2 14 20
Expected
9 6 6
Explanation
The flagged ids are {14, 20}. Scanning the convoy 9, 14, 6, 14, 20, 6: keep 9, drop 14, keep 6, drop 14, drop 20, keep 6. The surviving wagons, in order, carry ids 9, 6, 6.
Example 2
Input
1 5 3 5 5 5
Expected
(empty)Explanation
The flagged list repeats the id 5 three times, but as a set it is just {5}. The convoy's single wagon carries id 5, which is flagged, so it is removed and no wagons survive; the output is an empty line.
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 →