Problem:
Score : 300 points

Problem Statement

You are given an integer sequence of length N, a_1,a_2,...,a_N.

For each 1≤i≤N, you have three choices: add 1 to a_i, subtract 1 from a_i or do nothing.

After these operations, you select an integer X and count the number of i such that a_i=X.

Maximize this count by making optimal choices.

Constraints

1≤N≤10^5

0≤a_i<10^5 (1≤i≤N)

a_i is an integer.

Input

The input is given from Standard Input in the following format:

N
a_1 a_2 .. a_N

Output

Print the maximum possible number of i such that a_i=X.

Sample Input 1

7
3 1 4 1 5 9 2

Sample Output 1

4

For example, turn the sequence into 2,2,3,2,6,9,2 and select X=2 to obtain 4, the maximum possible count.

Sample Input 2

10
0 1 2 3 4 5 6 7 8 9

Sample Output 2

3

Sample Input 3

1
99999

Sample Output 3

1

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <iostream>
#include <stdlib.h>
#include <algorithm>

using namespace std;

int n;

//How many integers have you got to satisfy the condition?
int success(int height, int* h) {
    int s = 0;
    for (int i = 0; i < n; i++) {
        if((int)abs(h[i] - height) <= 1) {
            s++;
        }
    }
    return s;
}

int main() {
    cin >> n; //Number of integers in the array.
    int h[n]; //Height array.
    int lo, hi;
    lo = 100001;
    hi = 0;
    for (int i = 0; i < n; i++) {
        cin >> h[i];
        if (lo > h[i]) {
            lo = h[i];
        }
        if (hi < h[i]) {
            hi = h[i];
        }
    }
    lo++;
    hi--;
    int mid1, mid2, part; //Ternary search.
    while (hi - lo > 3) {
        part = (hi - lo) / 3;
        mid1 = lo + part;
        mid2 = hi - part;
        if (max(success(mid1, h), success(mid1 - 3, h)) > max(success(mid2, h), success(mid2 + 3, h))) {
            hi = mid2;
        } else {
            lo = mid1;
        }
    }
    lo = lo - 4;
    hi = hi + 4;
    int maxS = success(lo, h);
    for (++lo; lo <= hi; lo++) {
        maxS = max(maxS, success(lo, h));
    }
    cout << maxS << endl;
    return 0;
}