Skip to content

Commit 70db21f

Browse files
authored
Create number-of-ways-where-square-of-number-is-equal-to-product-of-two-numbers.cpp
1 parent 4fd8696 commit 70db21f

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Time: O(m * n)
2+
// Space: O(m + n)
3+
4+
class Solution {
5+
public:
6+
int numTriplets(vector<int>& nums1, vector<int>& nums2) {
7+
int result = 0;
8+
for (const auto& n : nums1) {
9+
result += twoProduct(nums2, static_cast<int64_t>(n) * n);
10+
}
11+
for (const auto& n : nums2) {
12+
result += twoProduct(nums1, static_cast<int64_t>(n) * n);
13+
}
14+
return result;
15+
}
16+
17+
private:
18+
int twoProduct(const vector<int>& nums, int64_t i) {
19+
unordered_map<int, int> lookup;
20+
int count = 0;
21+
for (const auto& num : nums) {
22+
if (i % num) {
23+
continue;
24+
}
25+
count += lookup[i / num];
26+
++lookup[num];
27+
}
28+
return count;
29+
}
30+
};

0 commit comments

Comments
 (0)