Skip to content

Commit 2528097

Browse files
authored
Merge pull request #244 from capricemoto/d2q1
soln of q1 of d2;
2 parents edaa567 + e3ec2c0 commit 2528097

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

  • Problems/Mathematics/Day-02/sol/Samarth
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <bits/stdc++.h>
2+
#define endl '\n'
3+
typedef long long ll;
4+
5+
using namespace std;
6+
7+
/*
8+
PROBLEM STATEMENT:
9+
Hossam woke up bored, so he decided to create an interesting array with his friend Hazem.
10+
11+
Now, they have an array a
12+
of n positive integers, Hossam will choose a number ai and Hazem will choose a number aj
13+
14+
.
15+
16+
Count the number of interesting pairs (ai,aj)
17+
18+
that meet all the following conditions:
19+
20+
1≤i,j≤n
21+
i≠j
22+
The absolute difference |ai−aj| must be equal to the maximum absolute difference over all the pairs in the array. More formally, |ai−aj|=max1≤p,q≤n|ap−aq|.
23+
24+
APPROACH:
25+
just count the freq of max &min elements and by basic combinotorics we get ans 2*fmax*fmin
26+
but if all elements are equal i.e fmax ==fmin then ans is n*(n-1) by basic combinototics too.
27+
28+
Time Complexity: O(n)
29+
Space complexity:O(n)
30+
SUBMISSION LINK:https://codeforces.com/problemset/submission/1771/355306251;
31+
*/
32+
33+
34+
35+
36+
void solve(){
37+
int n;cin>>n;int a[n];int max=INT_MIN,min=INT_MAX,fmax=0,fmin=0;
38+
for(int i=0;i<n;i++){
39+
cin>>a[i];
40+
if(a[i]>max){
41+
max=a[i];fmax=0;}
42+
if(a[i]<min){
43+
min=a[i];fmin=0;}
44+
if(a[i]==max)fmax++;
45+
if(a[i]==min)fmin++;
46+
}
47+
ll ans;
48+
if(max==min)ans=((long long)(n)*(long long)(n-1));
49+
else ans=2*(ll)fmax*(ll)fmin;
50+
cout<<ans<<endl;
51+
}
52+
53+
int main(){
54+
ios::sync_with_stdio(0); cin.tie(0);
55+
int t;cin>>t;
56+
while(t--)
57+
solve();
58+
59+
}

0 commit comments

Comments
 (0)