C++11 STL std::unordered_map -- Map with Square as Key & Repeat Squares as Value
Description\n\nEssentially, this solution breaks the problem into:\n1. Creating an unordered_map with squares as key and the value storing the number of times t
# Description\n\nEssentially, this solution breaks the problem into:\n1. Creating an `unordered_map` with squares as key and the value storing the number of times the same square has been found (this is why we couldn\'t use an `unordered_set` here).\n2. Searching across the keys of the squares maps for matches with the product of two indexes inside of nums1 and nums2 in $$O( \\frac {n (n-1)} {2} )$$, since "**Note**" allows this instead of $$O(n^3)$$ if using three nested loops, or $$O(n^2 log \\space n)$$ using standard maps.\n\n**Note:** Using the `unordered_` flavor is important, as it allows $$O(1)$$ retrieval by employing hashing! A standard `map` could be slower due to $$O(log \\space n)$$ ordered retrieval time, which could add up for `.find()` occurring many times.\n\n# Code\n```\nclass Solution {\npublic:\n    int numTriplets(vector<int>& nums1, vector<int>& nums2) {\n        unordered_map<uint64_t, int> squares1, squares2;\n        squares1.reserve(nums1.size());\n        squares2.reserve(nums2.size());\n\n        int found = 0;\n\n        // (1)\n        for (uint64_t bignum : nums1) {\n            bignum *= bignum;\n            squares1[bignum]++;\n        }\n\n        for (uint64_t bignum : nums2) {\n            bignum *= bignum;\n            squares2[bignum]++;\n        }\n\n        // (2)\n        for (int i = 0; i < nums1.size() - 1; i++)\n            for (int j = i + 1; j < nums1.size(); j++) {\n                uint64_t bignum = static_cast<uint64_t>(nums1[i]) * static_cast<uint64_t>(nums1[j]);\n                if (squares2.find(bignum) != squares2.end())\n                    found += squares2[bignum];\n            }\n\n        for (int i = 0; i < nums2.size() - 1; i++)\n            for (int j = i + 1; j < nums2.size(); j++) {\n                uint64_t bignum = static_cast<uint64_t>(nums2[i]) * static_cast<uint64_t>(nums2[j]);\n                if (squares1.find(bignum) != squares1.end())\n                    found += squares1[bignum];\n            }\n\n        return found;\n    }\n};\n```
['Math', 'Hash Function', 'C++']