Skip to content

Commit e8f9e60

Browse files
authored
Merge pull request #247 from icyfire8/patch-2
Add Solution1.cpp for interesting pairs problem
2 parents 2528097 + bbfcedc commit e8f9e60

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*https://codeforces.com/contest/1771/submission/355306957
2+
PROBLEM STATEMENT:
3+
Hossam woke up bored, so he decided to create an interesting array with his friend Hazem.
4+
5+
They are given an array 'a' of 'n' positive integers.
6+
Hossam chooses an element a[i] and Hazem chooses an element a[j].
7+
8+
An ordered pair (a[i], a[j]) is considered interesting if it satisfies:
9+
1) 1 ≤ i, j ≤ n
10+
2) i ≠ j
11+
3) The absolute difference |a[i] − a[j]| is equal to the maximum absolute
12+
difference among all possible pairs in the array, i.e.,
13+
|a[i] − a[j]| = max₁≤p,q≤n |a[p] − a[q]|
14+
15+
Input:
16+
- The first line contains an integer t (1 ≤ t ≤ 100), representing the number of test cases.
17+
- For each test case:
18+
- The first line contains an integer n (2 ≤ n ≤ 10⁵).
19+
- The second line contains n integers a₁, a₂, …, aₙ (1 ≤ aᵢ ≤ 10⁵).
20+
21+
Constraints:
22+
- The total sum of n across all test cases does not exceed 10⁵.
23+
24+
Output:
25+
- For each test case, output the number of interesting pairs (a[i], a[j]).
26+
*/
27+
28+
//T.C.:O(t.nlogn)
29+
//S.C.:O(n)
30+
31+
#include <bits/stdc++.h>
32+
#include <vector>
33+
using namespace std;
34+
35+
int main() {
36+
long long t, n, i, j, f, e, diff;
37+
cin >> t;
38+
39+
while(t--){
40+
cin >> n;
41+
vector <long long> v(n);
42+
43+
for(i=0; i<n; i++) cin >> v[i];
44+
45+
f=0, e=0;
46+
47+
sort(v.begin(),v.end()); //sorting the array from ascending to descending
48+
diff = v[n-1] - v[0]; //maximum absolute difference
49+
50+
if(diff == 0) cout << n*(n-1) << endl; //if same element is present throughout the array
51+
52+
else {
53+
for(i=0; i<n; i++){
54+
if(v[i] == v[0]) f++; //counting no. of smallest duplicate elements
55+
if(v[i] == v[n-1]) e++; //counting no. of largest duplicate elements
56+
}
57+
58+
cout << 2*f*e << endl; //2 times the no. of pairs
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)