1577. Number of Ways Where Square of Number Is Equal to Product of Two Numbers
Description
Given two arrays of integers nums1 and nums2, return the number of triplets formed (type 1 and type 2) under the following rules:
- Type 1: Triplet (i, j, k) if
nums1[i]2 == nums2[j] * nums2[k]where0 <= i < nums1.lengthand0 <= j < k < nums2.length. - Type 2: Triplet (i, j, k) if
nums2[i]2 == nums1[j] * nums1[k]where0 <= i < nums2.lengthand0 <= j < k < nums1.length.
Example 1:
Input: nums1 = [7,4], nums2 = [5,2,8,9] Output: 1 Explanation: Type 1: (1, 1, 2), nums1[1]2 = nums2[1] * nums2[2]. (42 = 2 * 8).
Example 2:
Input: nums1 = [1,1], nums2 = [1,1,1] Output: 9 Explanation: All Triplets are valid, because 12 = 1 * 1. Type 1: (0,0,1), (0,0,2), (0,1,2), (1,0,1), (1,0,2), (1,1,2). nums1[i]2 = nums2[j] * nums2[k]. Type 2: (0,0,1), (1,0,1), (2,0,1). nums2[i]2 = nums1[j] * nums1[k].
Example 3:
Input: nums1 = [7,7,8,3], nums2 = [1,2,9,7] Output: 2 Explanation: There are 2 valid triplets. Type 1: (3,0,2). nums1[3]2 = nums2[0] * nums2[2]. Type 2: (3,0,1). nums2[3]2 = nums1[0] * nums1[1].
Constraints:
1 <= nums1.length, nums2.length <= 10001 <= nums1[i], nums2[i] <= 105
Solutions
Solution 1: Hash Table + Enumeration
Thinking
Count triples with \(nums1[i]^2=nums2[j]\cdot nums2[k]\) and the symmetric case. Lengths are typically about \(100\), so a triple loop would pass, yet the same product is requested many times.
Hash every unordered pair-product on one side, then query \(x^2\) for each \(x\) on the other side. Swap the two roles and add the counts.
We use a hash table \(\textit{cnt1}\) to count the occurrences of each pair \((\textit{nums}[j], \textit{nums}[k])\) in \(\textit{nums1}\), where \(0 \leq j < k < m\), and \(m\) is the length of the array \(\textit{nums1}\). Similarly, we use a hash table \(\textit{cnt2}\) to count the occurrences of each pair \((\textit{nums}[j], \textit{nums}[k])\) in \(\textit{nums2}\), where \(0 \leq j < k < n\), and \(n\) is the length of the array \(\textit{nums2}\).
Next, we enumerate each number \(x\) in the array \(\textit{nums1}\) and calculate the value of \(\textit{cnt2}[x^2]\), which is the number of pairs \((\textit{nums}[j], \textit{nums}[k])\) in \(\textit{nums2}\) that satisfy \(\textit{nums}[j] \times \textit{nums}[k] = x^2\). Similarly, we enumerate each number \(x\) in the array \(\textit{nums2}\) and calculate the value of \(\textit{cnt1}[x^2]\), which is the number of pairs \((\textit{nums}[j], \textit{nums}[k])\) in \(\textit{nums1}\) that satisfy \(\textit{nums}[j] \times \textit{nums}[k] = x^2\). Finally, we return the sum of the two results.
The time complexity is \(O(m^2 + n^2 + m + n)\), and the space complexity is \(O(m^2 + n^2)\). Here, \(m\) and \(n\) are the lengths of the arrays \(\textit{nums1}\) and \(\textit{nums2}\), respectively.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
Solution 2: Hash Table + Enumeration Optimization
Thinking
Solution 1 stores every pair product and uses \(O(n^2)\) memory. Storing frequencies instead, we enumerate \(x\) and a value \(y\), set \(z=x^2/y\) when it divides, and add \(v_y(v_z-[y=z])\), then divide by two to drop the \((y,z)/(z,y)\) swap. Time becomes \(O(mn)\) with linear space.
We use a hash table \(\textit{cnt1}\) to count the occurrences of each number in \(\textit{nums1}\), and a hash table \(\textit{cnt2}\) to count the occurrences of each number in \(\textit{nums2}\).
Next, we enumerate each number \(x\) in the array \(\textit{nums1}\), and then enumerate each pair \((y, v1)\) in \(\textit{cnt2}\), where \(y\) is the key of \(\textit{cnt2}\) and \(v1\) is the value of \(\textit{cnt2}\). We calculate \(z = x^2 / y\). If \(y \times z = x^2\), and if \(y = z\), it means \(y\) and \(z\) are the same number, then the number of ways to choose two numbers from \(v1\) is \(v1 \times (v1 - 1) = v1 \times (v2 - 1)\). If \(y \neq z\), then the number of ways to choose two numbers from \(v1\) is \(v1 \times v2\). Finally, we sum all the ways and divide by \(2\). The division by \(2\) is because we count the number of ways for the pair \((j, k)\), but \((j, k)\) and \((k, j)\) are the same way.
The time complexity is \(O(m \times n)\), and the space complexity is \(O(m + n)\). Here, \(m\) and \(n\) are the lengths of the arrays \(\textit{nums1}\) and \(\textit{nums2}\), respectively.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |