1726: Tuple-with-Same-Product
Medium
I like to hash out a plan to map out my solution for upcoming leetcode questions. Yes, it is a hash-map problem.
Problem itself is quite straightforward once you see the vision:
Code:
class Solution {
public:
int tupleSameProduct(vector<int>& nums) {
<int, int> mp;
unordered_mapint ans = 0;
for (int i = 0; i < nums.size(); ++i) {
for (int j = i+1; j < nums.size(); ++j) {
++mp[nums[i] * nums[j]];
}
}
for (auto &multiple : mp) {
+= (multiple.second * (multiple.second-1)) * 4;
ans }
return ans;
}
};