Class Solution

  • All Implemented Interfaces:

    
    public final class Solution
    
                        

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

    Medium

    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 <code>nums1i<sup>2</sup> == nums2j * nums2k</code> where 0 &lt;= i &lt; nums1.length and 0 &lt;= j &lt; k &lt; nums2.length.

    • Type 2: Triplet (i, j, k) if <code>nums2i<sup>2</sup> == nums1j * nums1k</code> where 0 &lt;= i &lt; nums2.length and 0 &lt;= j &lt; k &lt; nums1.length.

    Example 1:

    Input: nums1 = 7,4, nums2 = 5,2,8,9

    Output: 1

    Explanation: Type 1: (1, 1, 2), nums11<sup>2</sup> = nums21 \* nums22. (4<sup>2</sup> = 2 \* 8).

    Example 2:

    Input: nums1 = 1,1, nums2 = 1,1,1

    Output: 9

    Explanation: All Triplets are valid, because 1<sup>2</sup> = 1 \* 1.

    Type 1: (0,0,1), (0,0,2), (0,1,2), (1,0,1), (1,0,2), (1,1,2). nums1i<sup>2</sup> = nums2j \* nums2k.

    Type 2: (0,0,1), (1,0,1), (2,0,1). nums2i<sup>2</sup> = nums1j \* nums1k.

    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). nums13<sup>2</sup> = nums20 \* nums22.

    Type 2: (3,0,1). nums23<sup>2</sup> = nums10 \* nums11.

    Constraints:

    • 1 &lt;= nums1.length, nums2.length &lt;= 1000

    • <code>1 <= nums1i, nums2i<= 10<sup>5</sup></code>

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      Solution()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description