【CF1154B】Make Them Equal 【数论】【 set集合实现 】

                                                    B. Make Them Equal

     time limit per test     2 seconds

    memory limit per test    256 megabytes

 

You are given a sequence a1,a2,…,ana1,a2,…,an consisting of nn integers.

You can choose any non-negative integer DD (i.e. D≥0D≥0), and for each aiai you can:

  • add DD (only once), i. e. perform ai:=ai+Dai:=ai+D, or
  • subtract DD (only once), i. e. perform ai:=ai−Dai:=ai−D, or
  • leave the value of aiai unchanged.

It is possible that after an operation the value aiai becomes negative.

Your goal is to choose such minimum non-negative integer DD and perform changes in such a way, that all aiai are equal (i.e. a1=a2=⋯=ana1=a2=⋯=an).

Print the required DD or, if it is impossible to choose such value DD, print -1.

For example, for array [2,8][2,8] the value D=3D=3 is minimum possible because you can obtain the array [5,5][5,5] if you will add DD to 22 and subtract DD from 88. And for array [1,4,7,7][1,4,7,7] the value D=3D=3 is also minimum possible. You can add it to 11 and subtract it from 77 and obtain the array [4,4,4,4][4,4,4,4].

Input

The first line of the input contains one integer nn (1≤n≤1001≤n≤100) — the number of elements in aa.

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1001≤ai≤100) — the sequence aa.

Output

Print one integer — the minimum non-negative integer value DD such that if you add this value to some aiai, subtract this value from some aiai and leave some aiai without changes, all obtained values become equal.

If it is impossible to choose such value DD, print -1.

Examples

input

Copy

6
1 4 4 7 4 1

output

Copy

3

input

Copy

5
2 2 5 2 5

output

Copy

3

input

Copy

4
1 3 3 7

output

Copy

-1

input

Copy

2
2 8

output

Copy

3

题意:

      给你n个数,是否存在一个数D,对每个数只操作一次(+D或-D或不变),是最后所有的元素都相同

思路:

       1:先对所有的数据排序和去重(set实现或自己手动用sort实现)

       2:对所有出现过的数来说,经过排序后,用cot记录出现的数字的个数

              cot==1,说明所有数据都一样,输出0

              cot==2,说明只出现了两种数,当两数之差是奇数就直接输出,是偶数的话就输出一半就好了(比如2,4来说输出1,2+1和4-1都等于3)

              cot==3,判断这三个数是否属于等差,是输出公差,否则输出-1

               cot>3,则不存在一个数使他们相同


代码:

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<math.h>
#include<string.h>
#include<stdlib.h>

#include<set>        ///头文件

const double PI=acos(-1);
const int INF=1e9+7;
const int xmax=1e5;
const int xmin=-1e6+7;
typedef long long ll;
using namespace std;

int main()
{
    set<int> st;
    int n;
    int a,tmp;
    int t[107],cot=0;
    cin>>n;
    st.clear();    ///集合set清空
    for(int i=1; i<=n; i++){
        cin>>a;
        st.insert(a);        ///加入集合中
    }
    set<int>::iterator it;    ///定义一个迭代器
    for(it=st.begin();it!=st.end();it++)
    {
        t[cot++]=*it;        ///把每个元素存入一个数组中
    }
    if(cot>3){
        printf("-1");
    }
    else if(cot==3)
    {
        tmp=t[1]-t[0];
        if(tmp==t[2]-t[1])
            printf("%d",tmp);
        else
            printf("-1");
    }
    else if(cot==2)
    {
        tmp=t[1]-t[0];
        if(tmp%2)
            printf("%d",tmp);
        else
            printf("%d",tmp/2);
    }
    else if(cot==1)
    {
        printf("0");
    }
    return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值