I don’t think there would be any division by zero, or ‘long long int’ to ‘int’ conversion in the following code:
(the code was a my submission for BUTYPAIR in yesterday’s (31/07/2021) July Lunchtime contest)
#include <iostream>
#include <vector>
#include <algorithm>
//#include <bits/stdc++.h>
using namespace std;
long long int fact(long long int N)
{
long long int fact = 1;
if(N>1)
for (long long int j = 2; j <= N; j++)
fact *= j;
return fact;
}
long long int permut(long long int n, long long int r)
{
long long int res=(fact(n) / fact(n - r));
return res;
}
int main() {
// your code goes here
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t;
cin >> t;
for (long long int tc = 0; tc < t; tc++)
{
long long int n, input;
long long int num_beauty_pairs;
cin >> n;
vector<long long int> arr;
num_beauty_pairs = permut(n, 2);
for (long long int i = 0; i < n; i++)
{
cin >> input;
arr.push_back(input);
}
sort(arr.begin(), arr.end());
long long int j = 1, count = 1;
while (j < n)
{
if (arr[j - 1] == arr[j])
{
count++;
}
else
{
if(count>1)
num_beauty_pairs -= permut(count, 2);
count = 1;
}
j++;
}
if (count > 1)
num_beauty_pairs -= permut(count, 2);
cout << num_beauty_pairs << "\n";
}
return 0;
}