You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
627 B
31 lines
627 B
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
|
|
int main() {
|
|
int n, k;
|
|
cin >> n >> k;
|
|
int *s = new int[n];
|
|
for (int i = 0; i < n; ++i) {
|
|
cin >> s[i];
|
|
}
|
|
//排序
|
|
sort(s, s + n);
|
|
|
|
int max = INT32_MIN;
|
|
for (int i = 0; i < n - 1; ++i) {
|
|
for (int j = i + 1; j < n; ++j) {
|
|
if (s[j] - s[i] <= k) {
|
|
if (j - i + 1 > max)max = j - i + 1;
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
//输出最多的个数
|
|
cout << max << endl;
|
|
//删除动态数组指针
|
|
delete[]s;
|
|
return 0;
|
|
}
|