Codechef |Coding Problem|Beautiful Pairs Problem

Input Format

Output Format

Constraints

Subtasks

Sample Input 1

2
3
4 2 4
6
2 8 6 2 1 5

Sample Output 1

4
28

Explanation

Code & Algorithm

/* author : @akash *//* 
problem is:-
*/#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mod 1000000007
#define ld long double
void solve()
{
ll n;
cin>>n;
ll a[n];
for(ll i=0;i<n;++i)
{
cin>>a[i];
}
map<ll,ll>m;
for(ll x:a)
{
m[x]++;
}
ll ans=n*(n-1);
for(auto y:m)
{
if(y.second>1)
{
ans-=y.second*(y.second-1);
}
}
cout<<ans;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--)
{
solve();
cout<<"\n";
}
return 0;
}
// time complexity of this algorithm is : T(n)=O(n)

--

--

--

Student of Computer Science & Engineering at Moradabad Institute of Technology.

Love podcasts or audiobooks? Learn on the go with our new app.

No Code — The Next Seismic Shift the Industry Needs

I just finished teaching a Full-Stack Bootcamp after 2 years. This is what I tell my students.

Airdrop Round 1 officially opened on September 20th at 12:00 PM UTC+7 with the HUGE number of…

How to Make Your First Open Source Contribution

Exit Full-Screen Implementation, Unity and C#

Simple Python Band Name Generator

Day 29 of 30-Day LeetCode Challenge

What is an API Contract?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Akash Kumar

Akash Kumar

Student of Computer Science & Engineering at Moradabad Institute of Technology.

More from Medium

Algorithm: Solve Sudoku

Before You Graduate! For Every Computer Science Student

The Linked List Data Structure

Spiral Matrix — Problem solution