FastPrepPythagorean Triple with Array Multiplicity

Pythagorean Triple with Array Multiplicity

ByteDance logoByteDance● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Given an integer array nums, return whether three distinct array occurrences can be chosen whose absolute values a, b, and c satisfy a * a + b * b = c * c.

  • Negative signs do not affect the squared values.
  • Equal absolute values may be reused only when the array contains enough separate occurrences.

Function

hasPythagoreanTriple(nums: int[]) → boolean

Examples

Example 1

nums = [3,-4,5,8]return = true

The distinct occurrences with absolute values 3, 4, and 5 satisfy 9 + 16 = 25.

Example 2

nums = [0,1,2]return = false

The single zero cannot be paired with two occurrences having equal absolute value, and no other triple satisfies the equation.

Constraints

  • 0 <= nums.length <= 2000.
  • -1000000000 <= nums[i] <= 1000000000.

More ByteDance problems

See ByteDance hiring insights
public boolean hasPythagoreanTriple(int[] nums) {
    // TODO: use three distinct array occurrences.
}
nums[3,-4,5,8]
expectedtrue
Checking account…