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) {
        unordered_map<int, int> mp;
        int 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) {
            ans += (multiple.second * (multiple.second-1)) * 4;
        }

        return ans;
    }
};

Complexity: