FastPrepFind All Pythagorean Triplets

Find All Pythagorean Triplets

Dell Technologies logoDell Technologies● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Given an integer array nums, return every unique value triplet [a, b, c] that can be formed from three different array positions and satisfies a * a + b * b = c * c.

Output rules

  • For this exercise, every input value is positive, so write each triplet with a <= b < c.
  • Return each value triplet only once, even if several index selections produce it.
  • Return the triplets in lexicographic order.

Function

findPythagoreanTriplets(nums: int[]) → int[][]

Examples

Example 1

nums = [3,4,5,6,8,10]return = [[3,4,5],[6,8,10]]

Both rows satisfy the square-sum equality and are already in lexicographic order.

Example 2

nums = [5,12,13,3,4,5,5]return = [[3,4,5],[5,12,13]]

Repeated values may create the same value triplet through several positions, but that row is returned once.

Constraints

  • 0 <= nums.length <= 2000.
  • 1 <= nums[i] <= 10^7.

More Dell Technologies problems

See Dell Technologies hiring insights
public int[][] findPythagoreanTriplets(int[] nums) {
    // Write your solution here.
}
nums[3,4,5,6,8,10]
expected[[3,4,5],[6,8,10]]
Checking account…