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 <bits/stdc++.h>

#define pii pair<int, int>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ll long long
#define ld long double
#define sz(v) int(v.size())
#define all(v) v.begin(), v.end()
#define vec vector<int>

#define left not_right
#define y1 what

using namespace std;

const int N = (int) 1e5 + 10;
const int M = (int) 2e6 + 7;
const ll LINF = (ll) 1e16;
const int INF = (int) 1e9 + 7;
const double EPS = (double) 1e-9;

int n;
ll pref[N], a[N];

ll get_low(int l, int r) {
	return a[r] * (r - l + 1) - (pref[r] - pref[l - 1]);
}

ll get_high(int l, int r) {
	return pref[r] - pref[l - 1] - a[l] * (r - l + 1);
}

int main() {
    #define fn "balls"
    #ifdef witch
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #else
//        freopen(fn".in", "r", stdin);
//        freopen(fn".out", "w", stdout);
    #endif
    srand(time(0));
    cin >> n;
    for (int i = 1; i <= n; i++) {
    	cin >> a[i];
    }
    sort(a + 1, a + n + 1);
    /*
    for (int i = 1; i <= n; i++) {
    	pref[i] = pref[i - 1] + a[i];
	}
    for (int i = 1, l = 1, r = 1; i <= n; i++) {
    	while (l < i && get_low(l, i) + get_high(i, r) > n) {
    		if (a[i] - a[l] > a[r] - a[i]) ++l;
    		else --r;
    	}
		while (r <= n && get_low(l, i) + get_high(i, r) <= n) {
    		++r;
    	}
    	cout << i << ' ' << l << ' ' << r - 1 << endl;
    	ans = max(ans, r - l);
    }
    */
	int ans = 0;
    for (int i = 1, l = 1, r = 1; i <= n; i++) {
    	while (abs(a[i] - a[l]) > 1) ++l;
    	while (abs(a[i] - a[r]) < 2) ++r;
    	ans = max(ans, r - l);
    }
    cout << ans;
    return 0;
}