pythagorean-triplets
algorithm-optimization
computational-efficiency
array-processing
mathematical-algorithms

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.

Practice algorithms

To efficiently find Pythagorean triplets in an array faster than the naive O(N2)O(N^2) 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 aa, bb, and cc, such that a2+b2=c2a^2 + b^2 = c^2. For example, (3,4,5)(3, 4, 5) is a well-known Pythagorean triplet because 32+42=523^2 + 4^2 = 5^2.

Naive Approach

The simple approach to find these triplets is to use three nested loops, which results in a time complexity of O(N3)O(N^3). An improvement over this is to fix one element and look for pairs that satisfy the condition, leading to O(N2)O(N^2) 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:

  1. Square Elements: First, square all the elements of the array. This allows us to directly work with the squared values.
  2. Use a `Hash` Set: Store these squared values in a hash set. This enables O(1)O(1) average time complexity for look-up operations.
  3. Find Triplets: For each possible pair (a,b)(a, b) in the squared array, calculate the sum of their squares, s=a2+b2s = a^2 + b^2. Check if ss exists in the hash set. If it does, then ((a,b,s(a, b, \sqrt{s}) or its permutations could be a valid Pythagorean triplet.

Algorithm

Here's the specific algorithm to find Pythagorean triplets faster than O(N2)O(N^2):

• 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 32+42=523^2 + 4^2 = 5^2. • 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.