AmazonOA #342
Server Selection
Estimated HardJudge-ready
Topics (AI-labelled)HashingGreedyTwo Pointers
Delete some elements from an integer array; the remaining elements can be rearranged freely into a ring in which every pair of adjacent elements, including the first and last, has an absolute difference ≤1. Find the maximum number of elements kept. Equal values can be used as many times as they occur, and no elements can be added.
Input
The first line contains n and the second line contains n integers. On this site 1≤n≤200000, −10^9≤value≤10^9.
Output
Output a single integer, the answer.
Example 1
Input:
6
4 3 5 1 2 1
Output:
3
Example 2
Input:
4
1 2 2 3
Output:
4
Example 3
Input:
1
5
Output:
1
Example notes
The three answers are 3, 4 and 1. The source's first example says 4, but its explanation conjures up an extra 2; with the given array at most 3 can be kept. In the second example, 1,2,3,2 does form a ring of length 4.
Editorial
Loading solution…