-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
38 lines (32 loc) · 929 Bytes
/
main.cpp
File metadata and controls
38 lines (32 loc) · 929 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
int tupleSameProduct(vector<int>& nums)
{
int size = (int)nums.size();
if (size < 4)
return 0;
unordered_map<int, int> frequencies; // (product, count)
for (int i = 0; i < size; ++i)
{
for (int j = i + 1; j < size; ++j)
{
int product = nums[i] * nums[j];
frequencies[product]++;
}
}
int distinct_tuples = 0;
for (auto& [product, count] : frequencies)
if (count > 1)
distinct_tuples += (count * (count - 1) / 2) * 8; // for a four element tuple there are 2^3=8 combinations
return distinct_tuples;
}
};
int main()
{
vector<int> nums = {1,2,4,5,10}; // expected 16
cout << "output: " << Solution().tupleSameProduct(nums) << '\n';
return 0;
}