Number of Ways Where Square of Number Is Equal to Product of Two Numbers - Problem

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]² == nums2[j] * nums2[k] where 0 <= i < nums1.length and 0 <= j < k < nums2.length.

Type 2: Triplet (i, j, k) if nums2[i]² == nums1[j] * nums1[k] where 0 <= i < nums2.length and 0 <= j < k < nums1.length.

Input & Output

Example 1 — Basic Case
$ Input: nums1 = [7,4], nums2 = [5,2,8,9]
Output: 1
💡 Note: Type 1: 7² = 49, but no pairs in nums2 multiply to 49. 4² = 16, but no pairs multiply to 16. Type 2: 5² = 25, no pairs in nums1 multiply to 25. 2² = 4, no pairs multiply to 4. 8² = 64, 7×9=63≠64. 9² = 81, no pairs multiply to 81. Wait, let me recalculate: 4² = 16, and 2×8 = 16, so we have triplet (1,1,2). Total = 1.
Example 2 — Multiple Matches
$ Input: nums1 = [1,1], nums2 = [1,1,1]
Output: 9
💡 Note: Type 1: 1² = 1. Pairs in nums2 that multiply to 1: (0,1), (0,2), (1,2) = 3 pairs. Each nums1 element (2 total) can form triplets: 2×3 = 6. Type 2: 1² = 1. Pairs in nums1 that multiply to 1: (0,1) = 1 pair. Each nums2 element (3 total): 3×1 = 3. Total = 6 + 3 = 9.
Example 3 — No Matches
$ Input: nums1 = [7,7,8,3], nums2 = [1,2,9,7]
Output: 2
💡 Note: Type 1: 7² = 49, 7×7 = 49 ✓ (but 7 appears once in nums2). 8² = 64, no valid pairs. 3² = 9, 1×9 = 9 ✓. Type 2: Check each nums2 element squared against nums1 pairs. After calculation, we get 2 valid triplets total.

Constraints

  • 1 ≤ nums1.length, nums2.length ≤ 1000
  • -105 ≤ nums1[i], nums2[i] ≤ 105

Visualization

Tap to expand
Square of Number = Product of Two Numbers INPUT nums1 = [7, 4] 7 4 i=0 i=1 nums2 = [5, 2, 8, 9] 5 2 8 9 Type 1: nums1[i]^2 == nums2[j]*nums2[k] Type 2: nums2[i]^2 == nums1[j]*nums1[k] Check: 4^2 = 16 2 * 8 = 16 [OK] Type 1 match found! i=1, j=1, k=2 ALGORITHM (Hash) 1 Build Product HashMap Store all pairs products nums2 products: 5*2=10, 5*8=40, 5*9=45 2*8=16, 2*9=18, 8*9=72 Map: {10:1, 40:1, 45:1, 16:1, 18:1, 72:1} 2 Calculate Squares For each nums1[i] 7^2=49, 4^2=16 Check if in hashmap 3 Lookup and Count Match squares to products 49 not in map --> 0 16 in map --> +1 4 Repeat for Type 2 Swap arrays, no matches Type 2: 0 triplets found FINAL RESULT Triplet Found: Type 1 Triplet (i=1, j=1, k=2) 4^2 = 2 * 8 Verification: nums1[1] = 4 4 * 4 = 16 2 * 8 = 16 [OK] Output: 1 Total triplets: 1 Key Insight: Use a HashMap to store product counts of all pairs (j,k) from one array. For each element i in the other array, lookup its square in the HashMap. This reduces O(n^2 * m) brute force to O(n^2 + m) using hash-based lookup. Process both Type 1 and Type 2 triplets by swapping roles of nums1 and nums2. TutorialsPoint - Number of Ways Where Square of Number Is Equal to Product of Two Numbers | Hash Approach
Asked in
Google 15 Amazon 12 Microsoft 8
12.0K Views
Medium Frequency
~25 min Avg. Time
456 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen