AmazonOA #60
Get Max Servers (Circular Adjacent Diff ≤ 1)
Estimated HardJudge-ready
Topics (AI-labelled)HashingTwo Pointers
Choose as many servers as possible and arrange them in a ring in any order so that the absolute power difference of every adjacent pair (including the first and last) is ≤1. Choosing just one server is allowed.
Input
The first line contains n (1..200000), and the second line contains n power values (0..10⁹).
Output
Output a single integer answer.
Example 1
Input:
7
4 3 5 1 2 2 1
Output:
5
Example 2
Input:
3
1 2 3
Output:
2
Example 3
Input:
2
7 7
Output:
2
Example notes
Example 1: choose 1, 1, 2, 2, 3 and arrange them as 1, 1, 2, 3, 2, for 5 servers; there is only one server with power 3, so the ring cannot cross over to 4 and come back. Example 2: each of the three values appears once, and the end values 1 and 3 differ by 2, so only 2 can be chosen. Example 3: two servers with the same value can both be chosen.
Editorial
Loading solution…