A festival crew has exactly m lanterns to hang along a bridge in m/2 mirrored pairs, where m is guaranteed to be even. Each lantern is marked with an integer color code. A pairing of all m lanterns is valid only if, in every one of the m/2 pairs, both lanterns carry exactly the same color code, and every lantern is used in exactly one pair. Determine whether the given list of color codes can be split into such pairs.
Line 1: an integer m, the total number of lanterns (m is even). Line 2: m space-separated integers, the color codes of the lanterns.
Print "YES" if a valid pairing exists, otherwise print "NO".
Example 1
Input
6 1 2 3 1 2 3
Expected
YES
Explanation
Code 1 appears twice, code 2 appears twice, and code 3 appears twice, so lanterns can be paired as (1,1), (2,2), (3,3) using every lantern -> YES.
Example 2
Input
4 1 2 3 4
Expected
NO
Explanation
Every code (1, 2, 3, 4) appears exactly once, an odd count, so no lantern has a matching partner and no valid pairing exists -> NO.
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 →