Re: [algogeeks] Amazon Question - Find Pythagorean triplet in an unsorted array

2011-10-13 Thread ravindra patel
@Wladimir, yeah I have heard about that. Another way of populating primitive pythagoreans is, for any natural number m > 1 (m^2 - 1, 2m, m^2 + 1) forms a pythagorean triplet. This is useful in populating pythagorean tiplets but here the problem is to search such triplets from a given int array. @

Re: [algogeeks] Inplace Array Convertion

2011-10-13 Thread Siddhartha Banerjee
if integers are positive,then go on a cycle... like a[2]goes to its final position, the element in a[2]'s final position goes to its final position, and so on... each time on visiting an element, put some marker on it... like make it negative... finally after an element comes to position of a[2]

[algogeeks] Inplace Array Convertion

2011-10-13 Thread shiva@Algo
Convert an array "a1 a2 a3...an b1 b2 b3...bn c1 c2 c3...cn" to "a1b1c1 a2b2c2...anbncn", inplace -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group,

Re: [algogeeks] Stone Game

2011-10-13 Thread Gaurav Kumar
Note that the problem says that the pile has AT LEAST i stones not exactly i stones. So it can for sure have more than i. On Thu, Oct 13, 2011 at 4:50 PM, Gaurav Kumar wrote: > I don't see this code considers the case when after throwing i stones, the > pile is still left with (Si-i) stones. For

Re: [algogeeks] Stone Game

2011-10-13 Thread Gaurav Kumar
I don't see this code considers the case when after throwing i stones, the pile is still left with (Si-i) stones. For example, let say pile 10 had 25 stones, now even after throwing 10 stones, pile 25 would be left with 15 stones, which could again be thrown by the next person. Am I missing somethi

Re: [algogeeks] Re: Given a String with unnecessary spaces, compact it in place

2011-10-13 Thread janani thiru
This solution doesn't work. It prints a blank string. I think because sometimes the condition becomes true only when a space is encountered. After which when we shift the value what happens to the value at the position where we are shifting from. It will still contain the char. This would be wron

Re: [algogeeks] Amazon Question - Find Pythagorean triplet in an unsorted array

2011-10-13 Thread Ankur Garg
@rahul...How do u choose z and x for computing z^2 -x^2 ? On Thu, Oct 13, 2011 at 11:34 PM, rahul wrote: > You can create a hash with sqrt(z2-x2). This will make it o(n). The > interviewer just made it lil tricky. That's all > > -- > You received this message because you are subscribed to the Go

[algogeeks] content extraction

2011-10-13 Thread karthikeya s
Does anyone have an idea about how to extract content from a file irrespective of file format??? -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group,

[algogeeks] Amazon Question - Find Pythagorean triplet in an unsorted array

2011-10-13 Thread rahul
You can create a hash with sqrt(z2-x2). This will make it o(n). The interviewer just made it lil tricky. That's all -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To view this discussion on the web visit https://groups.google.com/d/msg/alg

Re: [algogeeks] Amazon Question - Find Pythagorean triplet in an unsorted array

2011-10-13 Thread Ankur Garg
BTW can we solve this by hashing..That is the only feasible solution which comes to my mind to reduce the time complexity ? On Thu, Oct 13, 2011 at 11:20 PM, Ankur Garg wrote: > Dude this is nothing but 3 sum problem > > http://en.wikipedia.org/wiki/3SUM > > Ask interviewer to check this link an

[algogeeks] Re: Algo for Search in a 2D Matrix

2011-10-13 Thread vikas
@Sunny, good catch, k X k approach wont work lets try to use matrix itself as heap heapify(int cr, int cc, int Nr, int Nc){ mr = cr; mc = cc; if(cr+1 < Nr) mr = cr+1 mc = cc; if(cc + 1 < Nc && min > A[cc+1][cr]) mr = cr; mc = cc+1; if(A[cr][cc] > A[mr][mc]){ swap(&A[cr][cc] ,

Re: [algogeeks] Amazon Question - Find Pythagorean triplet in an unsorted array

2011-10-13 Thread praveen raj
N2 would me minimum On 13-Oct-2011 11:08 PM, "ravindra patel" wrote: > Hi, > Another question I faced in Amazon F2F. > > Given an unsorted array of integers, find all triplets that satisfy x^2 + > y^2 = z^2. > > For example if given array is - 1, 3, 7, 5, 4, 12, 13 > The answer should be - >

Re: [algogeeks] Amazon Question - Find Pythagorean triplet in an unsorted array

2011-10-13 Thread Ankur Garg
Dude this is nothing but 3 sum problem http://en.wikipedia.org/wiki/3SUM Ask interviewer to check this link and say he has gone mad!! :P Regards Ankur On Thu, Oct 13, 2011 at 10:29 PM, ravindra patel wrote: > Hi, > Another question I faced in Amazon F2F. > > Given an unsorted array of inte

[algogeeks] Re: Algo for Search in a 2D Matrix

2011-10-13 Thread vikas
@Sunny, good catch, k X k approach wont work lets try to use matrix itself as heap heapify(int cr, int cc, int Nr, int Nc){ mr = cr; mc = cc; if(cr+1 < Nr) mr = cr+1 mc = cc; if(cc + 1 < Nc && min > A[cc+1][cr]) mr = cr; mc = cc+1; if(A[cr][cc] > A[mr][mc]){ swap(&A[cr][cc] ,

Re: [algogeeks] Amazon Question - Find Pythagorean triplet in an unsorted array

2011-10-13 Thread Wladimir Tavares
I thinking in this property but i dont know how to use :( Euclid, in his book Elements, demonstrated that there is a infinnity of suits early Pythagoreans. Moreover, he found a formula that generates all primitive Pythagorean suits. Given two natural numbers m> n, the suit (a, b, c), where:

[algogeeks] Amazon Question - Find Pythagorean triplet in an unsorted array

2011-10-13 Thread ravindra patel
Hi, Another question I faced in Amazon F2F. Given an unsorted array of integers, find all triplets that satisfy x^2 + y^2 = z^2. For example if given array is - 1, 3, 7, 5, 4, 12, 13 The answer should be - 5, 12, 13 and 3, 4, 5 I suggested below algo with complexity O(n^2) - - Sort the

[algogeeks] Re: Given a String with unnecessary spaces, compact it in place

2011-10-13 Thread Don
@pradad 1. Never put strlen in the condition of a loop. That instantly makes an O(n) loop into O(n^2). 2. Be sure to put the null terminator at the end of the compacted string. Your current code stops one short of doing that. 3. Put {} around the body of the for loop if the body contains more than

[algogeeks] Re: How to Solve This

2011-10-13 Thread Gene
I should have noted that this can handle inputs up to about 2^32 / (10 * x). Run time is proportional to the number of 1's. You can also add a bit of code to discover the digits of the multiplicand. I was able to verify with lisp bignums that: 25,514 1's is equal to 76543 * ( a 25,509 digit numb

Re: [algogeeks] Re: Algo for Search in a 2D Matrix

2011-10-13 Thread sunny agrawal
Nopes Still it does not ensure that duplicates will not be there in the priority queue what i mean is you cannot write directly do k times{ data = pop() // let i,j are row and col of data push(i+1,j); push(i,j+1); } you need to add the following checks if((i+1,j) is not in the heap) push(i+1,j)

Re: [algogeeks] Re: Algo for Search in a 2D Matrix

2011-10-13 Thread anshu mishra
struct data { int row, col, int val; }; priority_queue heap; now fine? -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks