题目传送门
题解
- 滑动窗口跑一下即可
AC-Code
#include <bits/stdc++.h>
using namespace std;const int maxn = 2e5 + 7;
int arr[maxn];
int main() {
int n;while (cin >> n) {
for (int i = 1; i <= n; ++i) {
cin >> arr[i];}sort(arr + 1, arr + 1 + n);int L = 1, R = 1;int ans = 0;while (R <= n) {
if (arr[R] - arr[L] > 5) {
++L;}ans = max(R - L + 1, ans);++R;}cout << ans << endl;}
}