1577. Number of Ways Where Square of Number Is Equal to Product of Two Numbers

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 1577

class Solution:
    def numTriplets(self, nums1: List[int], nums2: List[int]) -> int:
        cnt1 = Counter(nums1)
        cnt2 = Counter(nums2)
        res = 0
        for a, x in cnt1.items():
            for b, y in cnt2.items():
                if a * a % b == 0:
                    c = a * a // b
                    if b == c:
                        res += x * y * (y - 1)
                    else:
                        res += x * y * cnt2[c]
                if b * b % a == 0:
                    c = b * b // a
                    if a == c:
                        res += x * (x - 1) * y
                    else:
                        res += x * y * cnt1[c]
        return res >> 1