Skip to content

Commit 8704a29

Browse files
authored
Merge pull request #215 from ViMo018/patch-2
Solved First Question of Day2
2 parents 71b343a + c719651 commit 8704a29

1 file changed

Lines changed: 66 additions & 0 deletions

File tree

  • Problems/Mathematics/Day-02/sol/Vishva
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include<bits/stdc++.h>
2+
#include<queue>
3+
using namespace std;
4+
typedef unordered_map<int, int> umii;
5+
typedef unordered_map<long long, long long> umll;
6+
typedef unordered_map<char, long long> umci;
7+
typedef vector<pair<int, int>> vpi;
8+
typedef vector<int> vi;
9+
typedef long long ll;
10+
typedef vector<long long> vll;
11+
typedef unordered_map<int , bool> umib;
12+
#define sum(v) accumulate(v.begin(), v.end(), 0)
13+
#define endl '\n'
14+
#define f0(i, n) for(long long i = 0; i < n; i++)
15+
#define f1(i, n) for(long long i = 1; i < n; i++)
16+
#define as(v) sort(v.begin(), v.end())
17+
#define all(x) (x).begin(), (x).end()
18+
#define pb push_back
19+
template<class T> umll frequency(vector<T> &v) {umll freq;for(auto &x:v) freq[x]++; return freq;}
20+
template<class T> umci S_frequency(vector<T> &v) {umci freq;for(auto &x:v) freq[x]++; return freq;}
21+
template <class T> void input(vector<T> &v){for(auto &x:v)cin>>x;}
22+
ll power(ll x, ll y){ ll res = 1; while (y > 0){ if (y & 1) res = (ll)(res*x); y = y>>1; x = (ll)(x*x); } return res; }
23+
void pvll(const vector<long long> &arr){for(auto it : arr){cout << it << " ";}cout << endl;}
24+
void pvi(const vector<int> &arr){for(auto it : arr){cout << it << " ";}cout << endl;}
25+
26+
27+
void solve(){
28+
29+
//-------------INPUT-------------
30+
ll n;
31+
cin >> n;
32+
vll v(n);
33+
input(v);
34+
sort(all(v));
35+
map<ll,ll>mp;
36+
ll req = abs(v[n-1]-v[0]);
37+
ll ans=0;
38+
for(ll i=0;i<n;i++)
39+
{
40+
ll target = (v[i]-req);
41+
if(mp.find(target)!=mp.end())
42+
{
43+
// cout << v[i] << " " << target << " ";
44+
ans+= (mp[target]);
45+
}
46+
mp[v[i]]++;
47+
}
48+
cout << 2*ans << endl;
49+
50+
// explanation :- this is basic 2 sum type question where map stores the frequency of previous elements
51+
// time complexity is o(nlogn)
52+
53+
// submission link:- https://codeforces.com/contest/1771/submission/355275950
54+
55+
56+
//-------------CODE--------------
57+
58+
59+
60+
}
61+
62+
63+
int main(){
64+
int tt; cin >> tt; while(tt--)
65+
{solve();};
66+
}

0 commit comments

Comments
 (0)