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

You're given two arrays of integers nums1 and nums2. Your task is to count the number of valid triplets that satisfy specific mathematical relationships.

There are two types of triplets to find:

  • Type 1: Triplet (i, j, k) where nums1[i]² = nums2[j] × nums2[k]
    Constraints: 0 ≤ i < nums1.length and 0 ≤ j < k < nums2.length
  • Type 2: Triplet (i, j, k) where nums2[i]² = nums1[j] × nums1[k]
    Constraints: 0 ≤ i < nums2.length and 0 ≤ j < k < nums1.length

Example: If nums1 = [7, 4] and nums2 = [5, 2, 8, 9], then 7² = 49 and we need to find if any two numbers in nums2 multiply to 49. We can't find such a pair, but 4² = 16 = 2 × 8, so we have one Type 1 triplet.

Goal: Return the total count of both types of triplets combined.

Input & Output

example_1.py — Basic Example
$ Input: nums1 = [7,4], nums2 = [5,2,8,9]
Output: 1
💡 Note: Type 1: 7² = 49. We need pairs from nums2 that multiply to 49. No such pairs exist. 4² = 16. We need pairs from nums2 that multiply to 16: (2,8) gives us 2×8=16. So 1 triplet: (1,1,2). Type 2: 5² = 25. No pairs in nums1 multiply to 25. 2² = 4. No pairs in nums1 multiply to 4. 8² = 64. No pairs in nums1 multiply to 64. 9² = 81. No pairs in nums1 multiply to 81. Total: 1
example_2.py — Multiple Matches
$ Input: nums1 = [1,1], nums2 = [1,1,1]
Output: 9
💡 Note: Type 1: 1² = 1. Pairs in nums2 that multiply to 1: (1,1). We have C(3,2) = 3 such pairs. For each 1 in nums1, we get 3 triplets. Total Type 1: 2×3 = 6. Type 2: 1² = 1. Pairs in nums1 that multiply to 1: (1,1). We have C(2,2) = 1 such pair. For each 1 in nums2, we get 1 triplet. Total Type 2: 3×1 = 3. Total: 6 + 3 = 9
example_3.py — No Valid Triplets
$ Input: nums1 = [7,7,8,3], nums2 = [1,2,9,7]
Output: 2
💡 Note: Type 1: 7² = 49. Pairs: (7,7) gives 49, but we need j < k, so no valid pairs. Wait, we have one 7 at index 3, so no pairs. Actually checking all: no pairs multiply to 49. 8² = 64. No pairs multiply to 64. 3² = 9. No pairs multiply to 9. Wait, let me recalculate... Actually, we need to check: 7×7=49 (not valid since we need j

Constraints

  • 1 ≤ nums1.length, nums2.length ≤ 1000
  • 1 ≤ nums1[i], nums2[i] ≤ 105
  • All calculations fit within standard integer ranges when using appropriate data types
  • Important: Watch for integer overflow when squaring large numbers

Visualization

Tap to expand
4²=16282 × 8 = 163²=9191 × 9 = 96²=36??No factors✓ Valid Triplet✓ Valid Triplet✗ No Valid Triplet
Understanding the Visualization
1
Identify the Pattern
We need to find when x² = y × z across different arrays
2
Count Element Frequencies
Build frequency maps to handle duplicate numbers efficiently
3
Factor Analysis
For each squared target, find all factor pairs and count their occurrences
4
Apply Combinatorics
Use C(n,2) for identical factors, multiply frequencies for different factors
Key Takeaway
🎯 Key Insight: The optimal solution leverages mathematical factor analysis combined with frequency counting to avoid generating all possible product pairs explicitly, resulting in a significant performance improvement from O(n³) to O(n√max).
Asked in
Google 25 Amazon 18 Microsoft 15 Meta 12
28.5K Views
Medium Frequency
~25 min Avg. Time
742 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