Given the feature of numerically adding a permutation to a sequence, elements of different values can become the same as long as their difference is strictly less than n n n.
This conclusion can be easily proven if we construct the worst case scenario: how to turn n n n consecutive numbers into the same value by adding a permutation. To do that, simply add the permutation in reverse order to achieve unanimous value. Moreover, it is evident that any subproblem of this form (less than n n n elements within the value range of n n n) can be solved in the same way.
Therefore, we can greedily implement a two-pointer algorithm that maintains an interval where the values differ less than n n n.
Additionally, the above method fails completely when the value of each element is not unique (i.e. duplicates). However, notice that duplicates can never result in the same value after adding a permutation. So, we simply get rid of all duplicates before the starting the above process.
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int Maxn=2e5+10;
int a[Maxn];
int n;
inline int read()
{
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0' && ch<='9')s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
return s*w;
}
int main()
{
// freopen("in.txt","r",stdin);
int T=read();
while(T--)
{
n=read();
for(int i=1;i<=n;++i)
a[i]=read();
sort(a+1,a+1+n);
int r=1,ans=1;
int cnt=unique(a+1,a+1+n)-a-1;
for(int i=1;i<cnt;++i)
{
r=max(r,i);
while(r<cnt && a[r+1]-a[i]<n)++r;
ans=max(ans,r-i+1);
}
printf("%d\n",ans);
}
return 0;
}