[algogeeks] Re: find triplets in an integer array A[] which satisfy condition: a[i]^2 + a[j]^2 = a[k]^2

2011-01-19 Thread bittu
Algo loop down array find 2 elements such that c= sqrt(a*a + b*b) & compare if( c*c ==a*a+b*b) //can be optimized we can leave this line just writing fro clarification if yes prints all the combination For This I Have A Gud Working Solution which has time complexity of O(n^2) Lets genralize

Re: [algogeeks] Re: find triplets in an integer array A[] which satisfy condition: a[i]^2 + a[j]^2 = a[k]^2

2010-12-01 Thread anoop chaurasiya
@DEV,your idea is nice.but i think it wont work in case the array is having repeated elementsam i right On Wed, Dec 1, 2010 at 1:51 PM, nishaanth wrote: > I have a O(n^2) solution.. > > Hash all the squares.0(n) > > Now for every pair find the sum, see if its there in the hash t

Re: [algogeeks] Re: find triplets in an integer array A[] which satisfy condition: a[i]^2 + a[j]^2 = a[k]^2

2010-12-01 Thread nishaanth
I have a O(n^2) solution.. Hash all the squares.0(n) Now for every pair find the sum, see if its there in the hash table.O(n^2) Total complexity : O(n^2) On Wed, Dec 1, 2010 at 11:00 PM, fenghuang wrote: > yeah, you're right. thank you and is it the optimal solution about this > questio

Re: [algogeeks] Re: find triplets in an integer array A[] which satisfy condition: a[i]^2 + a[j]^2 = a[k]^2

2010-12-01 Thread fenghuang
yeah, you're right. thank you and is it the optimal solution about this question? On Thu, Dec 2, 2010 at 1:08 AM, Dave wrote: > @Fenghuang: No. You don't have to search for b for every a, or more > precisely, you don't have to search for a j for every i. Something > like this should work for s

[algogeeks] Re: find triplets in an integer array A[] which satisfy condition: a[i]^2 + a[j]^2 = a[k]^2

2010-12-01 Thread Dave
@Fenghuang: No. You don't have to search for b for every a, or more precisely, you don't have to search for a j for every i. Something like this should work for step 3: i = 0 j = k-1 repeat while i <= j if asq[i] + asq[j] < asq[k] then i = i+1 else if asq[i] + asq[j] > asq[k] then j = j-1