A night-market light designer has strung up a long row of numbered paper lanterns for a festival. Every lantern starts switched off. Over the course of the evening, a control box receives a sequence of toggle commands; each command names one lantern by its id and flips that lantern's state — off becomes on, on becomes off. Lantern ids are not necessarily consecutive or nearby, and the same id can be sent many times throughout the evening.
Given the full sequence of commands in the order they were issued, determine which lanterns are lit once every command has been applied.
The first line contains a single integer q, the number of toggle commands. The second line contains q space-separated integers, the lantern id targeted by each command, in the order the commands were issued.
Print two lines. The first line contains a single integer, the number of lanterns that are lit after all commands have been applied. The second line contains the ids of those lanterns, in increasing order, separated by single spaces (print an empty line if no lantern ends up lit).
Example 1
Input
5 3 5 3 7 5
Expected
1 7
Explanation
Lantern 3 is toggled twice (on, then off), lantern 5 is toggled twice (on, then off), and lantern 7 is toggled once (on). Only lantern 7 ends up lit, so the answer is a count of 1 followed by "7".
Example 2
Input
4 1 2 1 2
Expected
0
Explanation
Lantern 1 is toggled twice and lantern 2 is toggled twice, so both end up back off. No lantern is lit, so the count is 0 and the second line is empty.
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 →