Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find pythagorean triplets in an array faster than O(N^2)?

Tags:

algorithm

Can someone suggest an algorithm that finds all Pythagorean triplets among numbers in a given array? If it's possible, please, suggest an algorithm faster than O(n2).

Pythagorean triplet is a set {a,b,c} such that a2 = b2 + c2. Example: for array [9, 2, 3, 4, 8, 5, 6, 10] the output of the algorithm should be {3, 4, 5} and {6, 8, 10}.

like image 900
Supriya Sane Avatar asked Jan 09 '10 02:01

Supriya Sane


People also ask

How do you find Pythagorean triples if given the smallest number?

H, P, B be the Pythagorean triplets. So, in order to find out Pythagorean triplet if on number is given use identity (a+b)2=(a−b)2+(2√ab)2and compare it with the side of right-angled triangle.

How do you find a Pythagorean triplet in C?

Show activity on this post. int a,b,n; printf("Input Natural Number n (n<2,100,000,000) : "); scanf("%d",&n); for(a=1;a<=100;a++) for(b=1;b<=100;b++) if(a<b && a*a + b*b == n*n) { printf("(%d, %d, %d)\n",a,b,n); } /*else { printf("impossible \n"); } */ return 0; if I delete 'else' the program runs correctly.


2 Answers

I understand this question as

Given an array, find all such triplets i,j and k, such that a[i]2 = a[j]2+a[k]2

The key idea of the solution is:

  • Square each element. (This takes O(n) time). This will reduce the original task to "find three numbers in array, one of which is the sum of other two".

Now it you know how to solve such task in less than O(n2) time, use such algorithm. Out of my mind comes only the following O(n2) solution:

  1. Sort the array in ascending order. This takes O(n log n).
  2. Now consider each element a[i]. If a[i]=a[j]+a[k], then, since numbers are positive and array is now sorted, k<i and j<i.

    To find such indexes, run a loop that increases j from 1 to i, and decreases k from i to 0 at the same time, until they meet. Increase j if a[j]+a[k] < a[i], and decrease k if the sum is greater than a[i]. If the sum is equal, that's one of the answers, print it, and shift both indexes.

    This takes O(i) operations.

  3. Repeat step 2 for each index i. This way you'll need totally O(n2) operations, which will be the final estimate.
like image 123
P Shved Avatar answered Oct 11 '22 14:10

P Shved


No one knows how to do significantly better than quadratic for the closely related 3SUM problem ( http://en.wikipedia.org/wiki/3SUM ). I'd rate the possibility of a fast solution to your problem as unlikely.


The 3SUM problem is finding a + b + c = 0. Let PYTHTRIP be the problem of finding a^2 + b^2 = c^2 when the inputs are real algebraic numbers. Here is the O(n log n)-time reduction from 3SUM to PYTHTRIP. As ShreevatsaR points out, this doesn't exclude the possibility of a number-theoretic trick (or a solution to 3SUM!).

First we reduce 3SUM to a problem I'll call 3SUM-ALT. In 3SUM-ALT, we want to find a + b = c where all array entries are nonnegative. The finishing reduction from 3SUM-ALT to PYTHTRIP is just taking square roots.

To solve 3SUM using 3SUM-ALT, first eliminate the possibility of triples where one of a, b, c is zero (O(n log n)). Now, any satisfying triple has two positive numbers and one negative, or two negative and one positive. Let w be a number greater than three times the absolute value of any input number. Solve two instances of 3SUM-ALT: one where all negative x are mapped to w - x and all positive x are mapped to 2w + x; one where all negative x are mapped to 2w - x and all positive x are mapped to w + x. The rest of the proof is straightforward.

like image 40
user287792 Avatar answered Oct 11 '22 15:10

user287792