Hossam and Combinatorics

给定一个长度为n的序列a,要求计算一共有多少对(i,j)使得abs(a[i]-a[j])=序列a中最大的abs(a[x]-a[y]),其中i!=j,x!=y;

Hossam woke up bored, so he decided to create an interesting array with his friend Hazem.

Now, they have an array aa of nn positive integers, Hossam will choose a number ai​ and Hazem will choose a number aj​.

Count the number of interesting pairs (ai​,aj​) that meet all the following conditions:

  • 1≤i,j≤n;
  • i!=j;
  • The absolute difference ∣ai​−aj​∣ must be equal to the maximum absolute difference over all the pairs in the array. More formally, ∣ai​−aj​∣=max1≤p,q≤n​∣ap​−aq​∣.

Input

The input consists of multiple test cases. The first line contains a single integer tt ( 1≤t≤100), which denotes the number of test cases. Description of the test cases follows.

The first line of each test case contains an integer nn (2≤n≤105).

The second line of each test case contains nn integers a1​,a2​,…,an​ (1 \le a_i \le 10^51≤ai​≤105).

It is guaranteed that the sum of nn over all test cases does not exceed 10^5105.

Output

For each test case print an integer — the number of interesting pairs(ai​,aj​).

Sample 1

InputOutput
2
5
6 2 3 8 1
6
7 2 8 3 2 10
2
4

 本题我们可以先求出最大最小值,然后求出最大最小值的数量下x,y。1.如果最大值等于最小值即输入的一组数中所有数相等,则输出等于(x-1)*y。2.如果最大值最小值不等,则输出等于x*y*2.(本题一定要注意数据范围)

 

int main()
{
	int t,n,max,min;
	long long j,k,x,y;
	scanf("%d",&t);
	while(t)
	{
		k=0;
		j=0;
		scanf("%d",&n);
		for(int i=0;i<n;i++)
		scanf("%d",&a[i]);
		max=a[0];
		min=a[0];
		for(int i=0;i<n;i++)
		{
			if(max<a[i])
			max=a[i];
			if(min>a[i])
			min=a[i];
		}
		for(int i=0;i<n;i++)
		{
		    if(a[i]==max)
			k++;
			if(a[i]==min)
			j++;	
		}
		x=(k-1)*j;
		y=k*j*2;
		if(max==min)
		printf("%lld\n",x);
		else
		printf("%lld\n",y);
		t--;
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值