287. The Longest Pick Where Values Differ By One
Choose as many values from the array as you like, in any positions. Your choice is HARMONIOUS when its largest and smallest values differ by exactly 1.
Print the size of the largest harmonious choice, or 0 if none exists.
Read the definition carefully, because it does most of the work. A difference of
exactly 1 means the choice contains exactly two distinct values, v and v + 1,
and every copy of both may be taken since duplicates change nothing about the
largest or smallest.
So the answer is the largest count(v) + count(v + 1) over values v where both
are present. One pass to count, one pass over the distinct values.
The trap is "exactly". An array of identical values has a difference of 0, not
1, so its answer is 0 rather than its length.
Constraints - `1 ≤ n ≤ 200000` - `-1000000000 ≤ value ≤ 1000000000`
Input
The first line contains an integer n.
The second line contains n space-separated integers.
Output
Print the size of the largest harmonious choice, or 0 if there is none.
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.