Solution by Dare2Solve | Detail Explanation | O(n * sqrt(max(x)))
Explanation []\nauthorslog.vercel.app/blog/H8FBC3F9WU\n\n\n# Code\n\nC++ []\nclass Solution {\npublic:\n    int numberOfPairs(vector<int>& nums1, vector<int>& n
```Explanation []\nauthorslog.vercel.app/blog/H8FBC3F9WU\n```\n\n# Code\n\n```C++ []\nclass Solution {\npublic:\n    int numberOfPairs(vector<int>& nums1, vector<int>& nums2, int k) {\n        unordered_map<int, int> map1;\n        unordered_map<int, int> map2;\n\n        for (int x : nums1) {\n            if (x % k == 0) {\n                int f = x / k;\n                map1[f]++;\n            }\n        }\n\n        for (int x : nums2) {\n            map2[x]++;\n        }\n\n        int res = 0;\n        for (const auto& [x, y] : map1) {\n            for (int i = 1; i <= sqrt(x); ++i) {\n                if (x % i == 0) {\n                    int cur = x / i;\n                    if (map2.find(i) != map2.end()) {\n                        res += map2[i] * y;\n                    }\n                    if (cur != i && map2.find(cur) != map2.end()) {\n                        res += map2[cur] * y;\n                    }\n                }\n            }\n        }\n\n        return res;\n    }\n};\n```\n\n```Python []\nclass Solution:\n    def numberOfPairs(self, nums1: List[int], nums2: List[int], k: int) -> int:\n        map1 = defaultdict(int)\n        map2 = defaultdict(int)\n\n        for x in nums1:\n            if x % k == 0:\n                f = x // k\n                map1[f] += 1\n\n        for x in nums2:\n            map2[x] += 1\n\n        res = 0\n        for x, y in map1.items():\n            for i in range(1, int(math.sqrt(x)) + 1):\n                if x % i == 0:\n                    cur = x // i\n                    if i in map2:\n                        res += map2[i] * y\n                    if cur != i and cur in map2:\n                        res += map2[cur] * y\n\n        return res\n```\n\n```Java []\nclass Solution {\n    public int numberOfPairs(int[] nums1, int[] nums2, int k) {\n        Map<Integer, Integer> map1 = new HashMap<>();\n        Map<Integer, Integer> map2 = new HashMap<>();\n\n        for (int x : nums1) {\n            if (x % k == 0) {\n                int f = x / k;\n                map1.put(f, map1.getOrDefault(f, 0) + 1);\n            }\n        }\n\n        for (int x : nums2) {\n            map2.put(x, map2.getOrDefault(x, 0) + 1);\n        }\n\n        int res = 0;\n        for (Map.Entry<Integer, Integer> entry : map1.entrySet()) {\n            int x = entry.getKey();\n            int y = entry.getValue();\n            for (int i = 1; i <= Math.sqrt(x); ++i) {\n                if (x % i == 0) {\n                    int cur = x / i;\n                    if (map2.containsKey(i)) {\n                        res += map2.get(i) * y;\n                    }\n                    if (cur != i && map2.containsKey(cur)) {\n                        res += map2.get(cur) * y;\n                    }\n                }\n            }\n        }\n\n        return res;\n    }\n}\n```\n\n```JavaScript []\n/**\n * @param {number[]} nums1\n * @param {number[]} nums2\n * @param {number} k\n * @return {number}\n */\nvar numberOfPairs = function (nums1, nums2, k) {\n  var map1 = new Map();\n  var map2 = new Map();\n\n  nums1.forEach((x) => {\n    if (x % k === 0) {\n      var f = x / k;\n      map1.set(f, (map1.get(f) || 0) + 1);\n    }\n  });\n\n  nums2.forEach((x) => {\n    map2.set(x, (map2.get(x) || 0) + 1);\n  });\n\n  var res = 0;\n  for (var [x, y] of map1) {\n    for (var i = 1; i <= Math.sqrt(x); ++i) {\n      if (x % i === 0) {\n        var cur = x / i;\n        if (map2.has(i)) res += map2.get(i) * y;\n        if (cur !== i && map2.has(cur)) res += map2.get(cur) * y;\n      }\n    }\n  }\n  return res;\n};\n```\n\n```Go []\nfunc numberOfPairs(nums1 []int, nums2 []int, k int) int {\n    map1 := make(map[int]int)\n    map2 := make(map[int]int)\n\n    for _, x := range nums1 {\n        if x % k == 0 {\n            f := x / k\n            map1[f] = map1[f] + 1\n        }\n    }\n\n    for _, x := range nums2 {\n        map2[x] = map2[x] + 1\n    }\n\n    var res int = 0\n    for x, y := range map1 {\n        for i := 1; i <= int(math.Sqrt(float64(x))); i++ {\n            if x % i == 0 {\n                cur := x / i\n                if count, ok := map2[i]; ok {\n                    res += count * y\n                }\n                if cur != i {\n                    if count, ok := map2[cur]; ok {\n                        res += count * y\n                    }\n                }\n            }\n        }\n    }\n\n    return res\n}\n```\n\n```C# []\npublic class Solution {\n    public int NumberOfPairs(int[] nums1, int[] nums2, int k) {\n        Dictionary<int, int> map1 = new Dictionary<int, int>();\n        Dictionary<int, int> map2 = new Dictionary<int, int>();\n\n        foreach (int x in nums1) {\n            if (x % k == 0) {\n                int f = x / k;\n                if (map1.ContainsKey(f))\n                    map1[f]++;\n                else\n                    map1[f] = 1;\n            }\n        }\n\n        foreach (int x in nums2) {\n            if (map2.ContainsKey(x))\n                map2[x]++;\n            else\n                map2[x] = 1;\n        }\n\n        int res = 0;\n        foreach (var entry in map1) {\n            int x = entry.Key;\n            int y = entry.Value;\n            for (int i = 1; i <= Math.Sqrt(x); ++i) {\n                if (x % i == 0) {\n                    int cur = x / i;\n                    if (map2.ContainsKey(i))\n                        res += map2[i] * y;\n                    if (cur != i && map2.ContainsKey(cur))\n                        res += map2[cur] * y;\n                }\n            }\n        }\n\n        return res;\n    }\n}\n```
['Hash Table', 'Python', 'C++', 'Java', 'Go', 'Python3', 'JavaScript', 'C#']