Skip to content

Commit a483453

Browse files
Create solution1.cpp
1 parent c5f45f3 commit a483453

1 file changed

Lines changed: 89 additions & 0 deletions

File tree

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*Problem
2+
Hossam woke up bored, so he decided to create an interesting array with his friend Hazem.
3+
4+
Now, they have an array a
5+
of n
6+
positive integers, Hossam will choose a number ai
7+
and Hazem will choose a number aj
8+
.
9+
10+
Count the number of interesting pairs (ai,aj)
11+
that meet all the following conditions:
12+
13+
1≤i,j≤n
14+
;
15+
i≠j
16+
;
17+
The absolute difference |ai−aj|
18+
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|
19+
.
20+
Input
21+
The input consists of multiple test cases. The first line contains a single integer t
22+
(1≤t≤100
23+
), which denotes the number of test cases. Description of the test cases follows.
24+
25+
The first line of each test case contains an integer n
26+
(2≤n≤105
27+
).
28+
29+
The second line of each test case contains n
30+
integers a1,a2,…,an
31+
(1≤ai≤105
32+
).
33+
34+
It is guaranteed that the sum of n
35+
over all test cases does not exceed 105
36+
.
37+
38+
Output
39+
For each test case print an integer — the number of interesting pairs (ai,aj)
40+
.
41+
Approach :
42+
find the largest difference which will be equal to maximum-minimum.
43+
check if all elements are same : if maximum=minimum then all elements same. and required ordered pairs will be simply = n*(n-1).
44+
if all are not same , then count the occurance of maximum and minimum since only these elements will contribute to required pairs.
45+
and no of pairs=maxcount*mincount*2. the extra 2 is for ordered pairs.
46+
*/
47+
// Codeforces Submission Link:
48+
// https://codeforces.com/contest/1771/submission/355653565
49+
50+
51+
52+
53+
54+
55+
#include <iostream>
56+
#include <climits>
57+
using namespace std;
58+
int main(){
59+
int t;
60+
cin>>t;
61+
while(t--){
62+
long long int n;
63+
cin>>n;
64+
long long int arr[n];
65+
long long int mini= INT_MAX;
66+
long long int cmini=0,cmaxi=0;
67+
long long int maxi=INT_MIN;
68+
for(long long int i=0;i<n;i++){
69+
cin>>arr[i];
70+
if(arr[i]>maxi){
71+
maxi=arr[i];
72+
}
73+
if(arr[i]<mini){
74+
mini=arr[i];
75+
}
76+
}
77+
if(maxi==mini){
78+
cout<<n*(n-1)<<endl;
79+
}
80+
else{
81+
for(long long int i=0;i<n;i++){
82+
if(arr[i]==maxi) cmaxi++;
83+
if(arr[i]==mini) cmini++;
84+
}
85+
cout<<2*cmaxi*cmini<<endl;
86+
}
87+
}
88+
return 0;
89+
}

0 commit comments

Comments
 (0)