A gem appraiser has a bag of n gemstones. Each gemstone is tagged with an integer color code. The appraiser wants to repackage all the gemstones into sealed parcels under these rules: every parcel must contain at least 2 gemstones, every parcel must contain the exact same number of gemstones as every other parcel, every parcel's gemstones must all share the same color code, and every gemstone in the bag must end up in exactly one parcel (none left over, none skipped).
Determine whether such a repackaging is possible for at least one valid parcel size.
Line 1: an integer n.
Line 2: n integers, the color code of each gemstone.
Print true if a valid repackaging exists, or false otherwise.
Example 1
Input
6 1 1 2 2 2 2
Expected
true
Explanation
Color 1 appears 2 times and color 2 appears 4 times. The greatest common divisor of 2 and 4 is 2, so parcels of size 2 work: one parcel of color 1, two parcels of color 2. Answer: true.
Example 2
Input
10 1 1 1 2 2 2 3 3 3 3
Expected
false
Explanation
Color 1 appears 3 times, color 2 appears 3 times, color 3 appears 4 times. The only common divisor of 3, 3, and 4 is 1, so no parcel size of 2 or more can divide all three counts evenly. Answer: false.
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 →