|
||||
Title: Longest Arithmetic Progression Post by Aryabhatta on Jul 9th, 2009, 10:56am Given an array of integers A, give an algorithm to find the longest Arithmetic progression in it, i.e find a sequence i1 < i2 < ... < ik, such that A[i1], A[i2], ..., A[ik] forms an arithmetic progression, and k is the largest possible. The sequence S1, S2, ..., Sk is called an arithmetic progression if Sj+1 - Sj is a constant. Note: just two elements can be considered to form an arithmetic progression. |
||||
Title: Re: Longest Arithmetic Progression Post by Aryabhatta on Jul 9th, 2009, 11:05am Apparently this has appeared before: http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi?board=riddles_cs;action=display;num=1214319368 but I don't think anyone has come up with an O(n^2) solution, there. |
||||
Title: Re: Longest Arithmetic Progression Post by towr on Jul 9th, 2009, 11:37am on 07/09/09 at 11:05:54, Aryabhatta wrote:
|
||||
Title: Re: Longest Arithmetic Progression Post by Aryabhatta on Jul 9th, 2009, 1:45pm on 07/09/09 at 11:37:52, towr wrote:
It seems like you reduced it to finding longest path in graphs. Is that accurate? Finding longest paths would be hard problem as we can solve Hamiltonian path with that... |
||||
Title: Re: Longest Arithmetic Progression Post by towr on Jul 9th, 2009, 2:21pm on 07/09/09 at 13:45:58, Aryabhatta wrote:
You can take all pairs of numbers; sort them in different bins depending on the difference (using, for example, a hash_map to keep this O(n2) ) Then for each collection of pairs of nodes, build a "graph", which consists of disjointed linked lists. Find the longest list. And that's it. You could do it in just one big graph of course, as long as distinct difference-edges have a different 'colour' or something. |
||||
Title: Re: Longest Arithmetic Progression Post by Aryabhatta on Jul 9th, 2009, 3:48pm on 07/09/09 at 14:21:40, towr wrote:
Using hash is still potentially worse than O(n^2) in worst case. There are algorithms which guarantee O(n^2) time. Anyway, what you have is acceptable. Sorry I didn't think about what you wrote carefully enough. |
||||
Title: Re: Longest Arithmetic Progression Post by spsyam1729 on Jul 10th, 2009, 2:13am I guess we can do straight Dynamic Programming with O(N^2) space, keeping all the current lengths we can solve this in O(N^2) time DP[i,j] will store length of AP from char 1 to i with j as the differential factor. |
||||
Title: Re: Longest Arithmetic Progression Post by towr on Jul 10th, 2009, 2:32am on 07/09/09 at 15:48:34, Aryabhatta wrote:
|
||||
Title: Re: Longest Arithmetic Progression Post by towr on Jul 10th, 2009, 2:33am on 07/10/09 at 02:13:35, spsyam1729 wrote:
Seems to me you need O(n2*d) where d is the maximum difference between two elements. |
||||
Title: Re: Longest Arithmetic Progression Post by TenaliRaman on Jul 10th, 2009, 8:26am We can use the following property here : If ..., a_{i+1}, a_i, ..., a_3, a_2, a_1, b, c_1, c_2, ..., c_i, c_{i+1}, ... is an A.P., then a_i + c_i = 2b Algo would be something like : S = sort(A) .. where A is the given array Consider each element in S as 'b' (as defined above) and move outward (on both sides) and keep finding all the A.P elements with b as center. One should be able to create a list 3-tuples and their A.P constants. Then, simply grouping by A.P constants and chaining these 3-tuples in each group should give the chain as well as their lengths. Simply get the one with max length from then on. It should be possible to do this in O(n^2), but I will try to check it properly later on. -- AI |
||||
Title: Re: Longest Arithmetic Progression Post by miletus on Jul 10th, 2009, 11:49am If we don't care about memory, we can use hash map + dp to achieve O(N^2) something like this Code:
|
||||
Title: Re: Longest Arithmetic Progression Post by TenaliRaman on Jul 11th, 2009, 4:00am The following is a Java code of the algorithm that I talked about earlier. Code:
-- AI P.S. -> I realize that I have done the tuple chaining slightly differently than what I had mentioned earlier. Here, I am chaining the tuples on the fly, whereas, earlier I mentioned about chaining the tuples after generating them. Both are valid as such. |
||||
Title: Re: Longest Arithmetic Progression Post by mistaken_id on Jul 12th, 2009, 5:39pm @TenaliRaman or anyone: If I give {3,4,5,2,1} as input to the algo what should the output be? a) 3,4,5 or b) 1,2,3,4,5 |
||||
Title: Re: Longest Arithmetic Progression Post by mistaken_id on Jul 12th, 2009, 5:54pm on 07/11/09 at 04:00:32, TenaliRaman wrote:
I think you need to remove duplicates for your algo to work correctly. |
||||
Title: Re: Longest Arithmetic Progression Post by mistaken_id on Jul 12th, 2009, 7:23pm Can we still find a O(n**2) when we want to find longest AP sequence in the original array but not in the sorted array. |
||||
Title: Re: Longest Arithmetic Progression Post by Grimbal on Jul 13th, 2009, 4:47am I think miletus's solution doesn't require the array to be sorted. It should work fine. |
||||
Title: Re: Longest Arithmetic Progression Post by Aryabhatta on Jul 13th, 2009, 12:48pm It is possible to do this without hashing/sorting. Here is an algorithm: http://compgeom.cs.uiuc.edu/~jeffe/pubs/pdf/arith.pdf |
||||
Title: Re: Longest Arithmetic Progression Post by TenaliRaman on Jul 14th, 2009, 1:14pm on 07/12/09 at 17:39:05, mistaken_id wrote:
I innately assumed (b) as the answer, but if you remove the sorting step from my code, I believe it should give you (a) as well. -- AI |
||||
Powered by YaBB 1 Gold - SP 1.4! Forum software copyright © 2000-2004 Yet another Bulletin Board |