A raffle drum holds a pile of numbered tickets. A ticket's face value v is called self-matching if exactly v tickets bearing that value are sitting in the drum. Given the full list of ticket values currently in the drum, determine the largest self-matching value. If no value in the drum is self-matching, report that none exists.
n, the number of tickets in the drum.n space-separated positive integers, the face value of each ticket.Print a single integer: the largest self-matching value, or -1 if no value in the drum is self-matching.
1 <= n <= 5001 <= value[i] <= 500Example 1
Input
4 2 2 3 4
Expected
2
Explanation
Value 2 appears twice (2 == 2, self-matching); value 3 appears once (3 != 1, not self-matching); value 4 appears once (4 != 1, not self-matching). The only self-matching value is 2, so that is the answer.
Example 2
Input
6 1 2 2 3 3 3
Expected
3
Explanation
Value 1 appears once (self-matching), value 2 appears twice (self-matching), and value 3 appears three times (self-matching). All three qualify, and the largest is 3.
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 →