C - Balanced Team
You are a coach at your local university. There are n students under your supervision, the programming skill of the i-th student is ai
.
You have to create a team for a new programming competition. As you know, the more students some team has the more probable its victory is! So you have to create a team with the maximum number of students. But you also know that a team should be balanced. It means that the programming skill of each pair of students in a created team should differ by no more than 5
.
Your task is to report the maximum possible number of students in a balanced team.
The first line of the input contains one integer n (1≤n≤2⋅105
) — the number of students.
The second line of the input contains n
integers a1,a2,…,an (1≤ai≤109), where ai is a programming skill of the i-th student.Print one integer — the maximum possible number of students in a balanced team.
Input
6
1 10 17 12 15 2
Output
3
Input
10
1337 1337 1337 1337 1337 1337 1337 1337 1337 1337
Output
10
题目大意:输入一组数,让你找出每个数之间差不大于5的数量。
**
**#include <iostream>
#include <algorithm>
using namespace std;
int a[200005];
int main()
{
long long int i,j,n,cou=1,sum=1;
cin>>n;
for(i=1; i<=n; ++i)
cin>>a[i];
sort(a+1,a+1+n);
for(i=1,j=2; i<=n&&j<=n&&i<=j;) //i<=j,it makes the sum will not be 0
{
if(a[j]-a[i]<=5)
j++,sum++;
else if(a[j]-a[i]>5)
i++,sum--;
cou=max(sum,cou);//!!!!!!!!
}
cout<<cou<<endl;//okay,max!
return 0;
}**
**