AC代码:
#define x first
#define y second
#define int long long
using namespace std;
const int N=4e5+5;
int n;
int a[N];
bool check(int k){
//cout<<"******"<<k<<endl;
map<int,int>mp;
for(int i=0;i<n;i++){
int x=(a[i]+k*N)%k;
mp[x]++;
}
for(auto t:mp)
if(t.y>=n/2)return true;
return false;
}
void solve(){
map<int,int>mp;
cin>>n;
for(int i=0;i<n;i++)cin>>a[i],mp[a[i]]++;
for(auto t:mp){
if(t.y>=n/2){
cout<<-1<<endl;
return;
}
}
vector<int>q;
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
q.push_back(abs(a[i]-a[j]));
int res=1;
for(int i=0;i<1000;i++){
int x=rand()%q.size();
int y=rand()%q.size();
//cout<<q[x]<<" "<<q[y]<<endl;
if(!q[x]&&!q[y])continue;
int k=__gcd(q[x],q[y]);
if(check(k))res=max(res,k);
}
cout<<res<<endl;
}
main(){
int T;
cin>>T;
while(T--)solve();
}