if the initial array is [9, 2, 3, -4, 8, 5, 6, 10]
square all the elements in the array O(n)
map all the squares and set as 1 if it is present in array O(n)
sort the array.. O(n log n)
i=0 to n-1
j=i+1 to n-1
if(map(arr[i]+arr[j])) then there exist a triplet
O(nlog n)
so totally this algo takes O(2n+2nlogn) ==> O(n log n)
On Sun, Aug 7, 2011 at 12:28 PM, programming love <
[email protected]> wrote:
>
> http://stackoverflow.com/questions/2032153/how-to-find-pythagorean-triplets-in-an-array-faster-than-on2
>
> It provides a perfect solution
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.