A - Minimum Modular

You have been given n distinct integers a1, a2, ..., an. You can remove at most k of them. Find the minimum modular m (m > 0), so that for every pair of the remaining integers (ai, aj), the following unequality holds: .

Input

The first line contains two integers n and k (1  ≤ n  ≤ 5000, 0 ≤ k ≤ 4), which we have mentioned above.

The second line contains n distinct integers a1, a2, ..., an (0 ≤ ai ≤ 106).

Output

Print a single positive integer — the minimum m.

Example
Input
7 0
0 2 3 6 7 12 18
Output
13
Input
7 1
0 2 3 6 7 12 18
Output
7
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include<iomanip>
using namespace std;
typedef long long ll;
#define inf int(0x3f3f3f3f)
#define mod int(1e9+7)
#define pi acos(-1)

const int maxn = 1e6 + 5;

int a[maxn],n,k,re[maxn],h[maxn];

int main()
{
    int ma=-1;
    scanf("%d%d",&n,&k);
    memset(re,0,sizeof(re));
    memset(h,0,sizeof(h));
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        if(ma<a[i]) ma=a[i];
    }
  for(int i=1;i<=n;i++)
  {
      for(int j=1;j<i;j++)
      {
          int tmp=a[i]-a[j]>0?a[i]-a[j]:a[j]-a[i];
          h[tmp]++;
      }
  }
  int res,flag=1;
  for(int m=1;m<=maxn+1;m++)
  {
      int tk=k;
      int sum=0;
      flag=1;
      for(int i=m;i<=1e6;i+=m)
      {
          sum+=h[i];
          if(sum>k*(k+1)/2) break;
      }
      if(sum>k*(k+1)/2) continue;
      res=m;
      for(int i=1;i<=n;i++)
      {
          if(!re[a[i]%res]) re[a[i]%res]++;
          else
            {
                tk--;
                if(tk<0) {flag=0;break;}
            }
      }
      memset(re,0,sizeof(re));
      if(flag) break;
  }
  printf("%d\n",res);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值