Skip to content

Commit 876067f

Browse files
authored
Merge pull request #287 from harjasbb07-eng/main
Added solution1.cpp from Harjas under Problems/Mathematics/Day-02/sol/
2 parents cca4879 + e04348e commit 876067f

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
}

0 commit comments

Comments
 (0)