CF 1771A - Hossam and Combinatorics
Hossam woke up bored, so he decided to create an interesting array with his friend Hazem.
Now, they have an array a of n 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 t (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 n (2≤n≤105).
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤105).
It is guaranteed that the sum of n over all test cases does not exceed 105.
Output
For each test case print an integer — the number of interesting pairs (ai,aj).
---------------------------------------------------------------
Hossam 醒來感到很無聊,所以他決定和他的朋友 Hazem 一起創(chuàng)建一個有趣的數(shù)組。
現(xiàn)在,他們有一個由 n 個正整數(shù)組成的數(shù)組 a,Hossam 將選擇一個數(shù)字 ai,Hazem 將選擇一個數(shù)字 aj。
計算滿足以下所有條件的有趣對 (ai,aj) 的數(shù)量:1≤i,j≤n;i≠j;
絕對差|ai?aj| 必須等于數(shù)組中所有對的最大絕對差。 更正式地說,|ai?aj|=max1≤p,q≤n|ap?aq|。
輸入
輸入由多個測試用例組成。 第一行包含一個整數(shù)t(1≤t≤100),表示測試用例的數(shù)量。 測試用例的描述如下。
每個測試用例的第一行包含一個整數(shù)n(2≤n≤105)。
每個測試用例的第二行包含n個整數(shù)a1,a2,…,an
? (1≤ai≤105)。
保證所有測試用例的n之和不超過105。
輸出
對于每個測試用例,打印一個整數(shù) - 有趣的對 (ai,aj) 的數(shù)量。
----------------------------
踩了2個坑,一個是乘積會溢出的情況,沒有看示例的數(shù)據(jù)范圍,一個是沒有考慮極端情況,也就是所有的值全部相當?shù)臅r候。
其他沒什么問題的,下面是代碼: