How to find pythagorean triplets in an array faster than ON2?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
To efficiently find Pythagorean triplets in an array faster than the naive approach, there are several advanced techniques and optimizations we can employ. In this article, we'll explore a method using hashing which significantly reduces the time complexity.
Understanding Pythagorean Triplets
A Pythagorean triplet consists of three positive integers , , and , such that . For example, is a well-known Pythagorean triplet because .
Naive Approach
The simple approach to find these triplets is to use three nested loops, which results in a time complexity of . An improvement over this is to fix one element and look for pairs that satisfy the condition, leading to using two nested loops. However, this can still be insufficient for large datasets.
Optimized Approach Using Hashing
To improve efficiency, we can utilize a hash map which helps in reducing the time complexity further. This approach relies on storing squares of array elements and verifying the triplet condition in fewer steps. Here's a step-by-step breakdown of the method:
- Square Elements: First, square all the elements of the array. This allows us to directly work with the squared values.
- Use a `Hash` Set: Store these squared values in a hash set. This enables average time complexity for look-up operations.
- Find Triplets: For each possible pair in the squared array, calculate the sum of their squares, . Check if exists in the hash set. If it does, then () or its permutations could be a valid Pythagorean triplet.
Algorithm
Here's the specific algorithm to find Pythagorean triplets faster than :
• Squaring values: `[9, 1, 16, 36, 25]` • `Hash` set of squared values: `{1, 9, 16, 36, 25}` • Triplet found during check: `(3, 4, 5)` since . • Duplicate Elements: The algorithm assumes elements are distinct unless handled differently. • Floating Points: When converting squared sums to roots, ensure proper rounding or integer casting to avoid floating-point pitfalls.
Related reading
- How to find repeating sequence of characters in a given array?
- How to find shortest path in dynamic situation
- How to find smallest substring which contains all characters from a given string?
- How to find Strongly Connected Components in a Graph?
- How to find rank of an element in stl set in Ologn
- How to Find the Branching Factor of a Tree
- How to find the center of a subset of vertices in a graph?
- how to find the least number of operations to compute xn

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.