File tree Expand file tree Collapse file tree
Problems/Mathematics/Day-02/sol/Harjas Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /*
2+ Problem Statement:
3+ Given size of array and elements. We need to find possible combination (i,j) of elements (i and j are positions in array)
4+ size that the absolute difference is equal to absolute max difference from elements of array
5+
6+ Prefix Sums:
7+ Not applicable since the problem is purely mathematical and does not involve
8+ range queries or cumulative sums.
9+
10+ Time Complexity: O(n)
11+ Space Complexity: O(n)
12+
13+ Submission Link:
14+ https://codeforces.com/contest/1771/submission/355453765
15+ */
16+ #include < bits/stdc++.h>
17+ using namespace std ;
18+
19+ int main (){
20+ int t;
21+ cin >>t;
22+ while (t--){
23+
24+ int n;
25+ cin>>n;
26+ vector <int > a (n);
27+ for (int &x: a) cin>> x;
28+
29+ int smallestNum= INT_MAX;
30+ int largestNum=INT_MIN;
31+ int counts =0 ;
32+ int countl=0 ;
33+ for (int i=0 ;i<n;i++){
34+ if (a[i]<smallestNum){
35+ smallestNum=a[i]; // scanning through array once to find number of times max and min element appears
36+ counts=0 ;
37+ }
38+ if (a[i]>largestNum){
39+ largestNum=a[i];
40+ countl=0 ;
41+ }
42+
43+ if (a[i]==smallestNum) counts++;
44+
45+ if (a[i]==largestNum) countl++;
46+ }
47+ if (smallestNum == largestNum) {
48+ cout << 1LL * n * (n - 1 ) << endl; // edge case when all elements are equal
49+ } else {
50+ cout << 2LL * counts * countl << endl; // final answer uses long long because n can be large so n^2 crosses max of int
51+ }
52+
53+
54+
55+
56+
57+
58+ }
59+ return 0 ;
60+ }
You can’t perform that action at this time.
0 commit comments