Codeforces Round #552 (Div. 3) B题

题目链接 http://codeforces.com/contest/1154/problem/B

题目

B. Make Them Equal

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

思路 暴力

AC代码

#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
    int n;
    int s[1005];
    while(cin>>n){
            int key;
        for(int i=0;i<n;i++)
            scanf("%d",&s[i]);
        sort(s,s+n);
        for(int i=0;i<100;i++){
            int ans=s[0]+i;
             key=1;
            for(int j=0;j<n;j++){
                if(s[j]-i==ans||s[j]==ans||s[j]+i==ans)
                    continue;
                else{
                    key=0;
                    break;
                }
            }
            if(key==1){
                cout<<i<<endl;
                break;
            }
        }
        if(key==0)
            cout<<"-1"<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值