Pythagorean Triplets in an Array
Updated
Problem statement
This is the three-sum problem with one change. Instead of three numbers whose sum is a target, you look for three numbers where the squares of two of them add up to the square of the third: a2 + b2 = c2. Given an integer array nums, find all such triplets.
Assumption: the three numbers of a triplet come from three different indices of nums, so a value can appear twice in a triplet only if nums holds it at two indices. nums may contain negative numbers and zeros; a number and its negative have the same square, but they count as different values, so [-3, 4, 5] and [3, 4, 5] are two different triplets.
Assumption: return the triplets as values, not indices. Inside each triplet, put the values in non-decreasing order. List each triplet only once, even when several choices of indices give the same values. Sort the list in ascending order, comparing triplets by their first value, then their second, then their third. If no triplet exists, return an empty list.
Function
findTriplets(nums: int[]) → List<List<Integer>>Examples
Example 1
nums = [5, -3, 4, 12, 13, 3]return = [[-3, 4, 5], [3, 4, 5], [5, 12, 13]]3, 4 and 5 form a triplet because 9 + 16 = 25. -3 has the same square as 3, so [-3, 4, 5] is a second triplet with different values. 5, 12 and 13 form a third one because 25 + 144 = 169; the 5 at index 0 takes part in all three triplets, which is allowed because each triplet is checked on its own. The list starts with [-3, 4, 5] because its first value is the smallest.
Example 2
nums = [0, 7, 7, 1]return = [[0, 7, 7]]0 + 49 = 49, so 0, one 7 and the other 7 form a triplet; the two 7s sit at indices 1 and 2. The values 0, 1, 1 would also satisfy the equation, but 1 appears at only one index, so it cannot be used twice.
Example 3
nums = [10, 6, 8, 6, 2]return = [[6, 8, 10]]36 + 64 = 100, so 6, 8 and 10 form a triplet. The 6 can be taken from index 1 or index 3; both choices give the same values, so [6, 8, 10] is listed once. No triplet contains 2.
Constraints
1 <= nums.length <= 3000-105 <= nums[i] <= 105
More Patreon Phone Screen questions
- Sliding-Window API Rate LimiterPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Employee Importance from a DictionaryPHONE SCREEN · Seen Feb 2026
- Uniform Random Number from Coin TossesPHONE SCREEN · Seen Feb 2026
- Wordle Guess CheckerPHONE SCREEN · Seen Jan 2026
- Org Chart Report CountsPHONE SCREEN · Seen Dec 2025
- Format the Missing Numbers from 1 to 99PHONE SCREEN · Seen Sep 2025
- LRU Cache with TTLPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2022
- Password GeneratorPHONE SCREEN · ONSITE INTERVIEW · Seen Mar 2022
- Merge IntervalsPHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2022
- JSON ParserPHONE SCREEN · Seen Jun 2021
Similar questions at other companies
- Employee Count Median, Mean and ModeClay · PHONE SCREEN
- Net Position Calculation from API SnapshotsHeadlands Technologies · ONSITE INTERVIEWHeadlands Technologies interview questions
- Top K Frequent ElementsAmazon · OA · PHONE SCREEN · ONSITE INTERVIEW
- Mutual-Friend RecommendationsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Filter, Sort, and Deduplicate Scheduled TasksRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Longest Consecutive SequenceJP Morgan Chase · PHONE SCREENJP Morgan Chase interview questions
- Top K Frequent Elements with a Recency TiebreakSwiggy · PHONE SCREEN
- Pins Violation Log QueriesPinterest · PHONE SCREENPinterest interview questions
- Group AnagramsWorkday · PHONE SCREEN
- Adaptive Calibration ErrorOpenAI · PHONE SCREENOpenAI interview questions