File tree Expand file tree Collapse file tree
Problems/Mathematics/Day-02/Lakshmish Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments