Easy to understand JavaScript solution
Complexity\n- Time complexity:\nO(n^2)\n\n- Space complexity:\nO(n)\n\n# Code\n\nvar numTriplets = function(nums1, nums2) {\n    const triplets = (numsA, numsB)
# Complexity\n- Time complexity:\n$$O(n^2)$$\n\n- Space complexity:\n$$O(n)$$\n\n# Code\n```\nvar numTriplets = function(nums1, nums2) {\n    const triplets = (numsA, numsB) => {\n        const squareMap = numsA.reduce((map, num) => {\n            const square = num ** 2;\n            const count = map.get(square) ?? 0;\n\n            return map.set(square, count + 1);\n        }, new Map());\n        let result = 0;\n\n        for (let a = 0; a < numsB.length; a++) {\n            for (let b = a - 1; b >= 0; b--) {\n                const product = numsB[a] * numsB[b];\n                if (!squareMap.has(product)) continue;\n                result += squareMap.get(product);\n            }\n        }\n        return result;\n    };\n\n    return triplets(nums1, nums2) + triplets(nums2, nums1);\n};\n```
['JavaScript']