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}
.
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.
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.
I understand this question as
Given an array, find all such triplets
i
,j
andk
, such that a[i]2 = a[j]2+a[k]2
The key idea of the solution is:
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:
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.
i
. This way you'll need totally O(n2) operations, which will be the final estimate.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With