Skip to content

Commit b97732a

Browse files
authored
Add solution for Codeforces problem 1771
Implement solution for Codeforces problem 1771 with input handling, sorting, and calculating maximum difference pairs.
1 parent c30b921 commit b97732a

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//https://codeforces.com/problemset/submission/1771/355264331
2+
//time complexity:O(t*n*nlogn)because of for loop and sort function
3+
//space complexity:O(1) not counting input
4+
#include <bits/stdc++.h>
5+
using namespace std;
6+
7+
int main() {
8+
int t;
9+
cin >> t;
10+
while (t--) {
11+
int n;
12+
cin >> n;
13+
vector <int> a;
14+
for (int i = 0; i < n; i++) {
15+
int x;
16+
cin >> x;
17+
a.push_back(x);//input
18+
}
19+
sort(a.begin(), a.end());//sorting
20+
long long last = 0;
21+
long long first = 0; // maximum difference is from first and last element
22+
if(a[0]==a[n-1]){
23+
24+
cout<<(n*(n-1))<<endl;//if all elements are same,max diff is 0 so we output number of ordered pairs
25+
}
26+
else {
27+
for (int i = 0; i < n; i++) {
28+
if (a[i] == a[n - 1]) {//number of last(high) element repeated
29+
last++;
30+
}
31+
else if (a[i] == a[0]) { // number of first(low)element repeated
32+
first++;
33+
}
34+
}
35+
36+
cout << 2 * last * first << endl; //2 indicates ordered pair
37+
}
38+
}
39+
40+
}

0 commit comments

Comments
 (0)