Codeforces Round #748 (Div. 3) D1. All are Same

题目链接:Problem - D1 - Codeforces

This problem is a simplified version of D2, but it has significant differences, so read the whole statement.

Polycarp has an array of nn (nn is even) integers a1,a2,…,an Polycarp conceived of a positive integer kk. After that, Polycarp began performing the following operations on the array: take an index ii (1≤i≤n) and reduce the number aiai by k.

After Polycarp performed some (possibly zero) number of such operations, it turned out that all numbers in the array became the same. Find the maximum k at which such a situation is possible, or print −1 if such a number can be arbitrarily large.

Input

The first line contains one integer t 1≤t≤10) — the number of test cases. Then tt test cases follow.

Each test case consists of two lines. The first line contains an even integer nn (4≤n≤40) (nn is even). The second line contains nn integers a1,a2,…an(−10^6≤ai≤10^6).

It is guaranteed that the sum of all nn specified in the given test cases does not exceed 100.

Output

For each test case output on a separate line an integer kk (k≥1) — the maximum possible number that Polycarp used in operations on the array, or −1, if such a number can be arbitrarily large.

Example

input

Copy

3
6
1 5 3 1 1 5
8
-1 0 1 -1 0 1 -1 0
4
100 -1000 -1000 -1000

output

Copy

2
1
1100

题意:可以选择一个值-k(任意次,可以0次),然后问把所有数字变成相同数字的最大k, 如果k可以为任意数出-1

思路:找到最小值,然后出去最小值的其他值-最小值的值求公因数,如果刚开始的值都相同就出-1


#include<bits/stdc++.h>
using namespace std;
 
int arr[45];
 
int main(){
	int t;
	cin >> t;
	while(t--){
		int n;
		int maxn = -1000005;
		int minn = 1000005;
		cin >> n;
		for(int i = 0; i < n; i++){
			cin >> arr[i];
			minn = min(minn, arr[i]);
			maxn = max(maxn, arr[i]);
		}
		int ans = maxn - minn;
		for(int i = 0; i < n; i++){
			if(arr[i] != minn){
				ans = __gcd(ans, arr[i] - minn);
			}
		}
		if(ans == 0){
			cout << -1 << endl;
			continue;
		}
		cout << ans << endl;
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值