HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Length of longest arithmetic progression in an array

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
arithmeticarraylengthlongestprogression

Problem

I was reading an article on Longest Arithmetic Progression. The solution given has S(n)=$O(n^2)$. Can't I solve it in $O(1)$ space? To find the three elements, we first fix an element as middle element and search for other two (one smaller and one greater). When we found them we still keep on decreasing left and right to find llap (length of longest arithmetic progression). The other cases left are those where llap is even.. for them I will select i and (i+1)$^{th}$ element as my center two elements of AP and find elements on their right and left satisfying ap. T(n) will still be $O(n^2)$.

Is my approach correct?

Solution

No, your approach is wrong. Consider the set of numbers $1, 3, 4, 6, 7, 8, 10$. The longest arithmetic progression(LAP) in it is $1, 4, 7, 10$, which is of even length. However, 4 and 7 are not adjacent items so your approach will not find that LAP.

Yes, your approach is correct, but to a different problem from the problem in the article you mentioned.

Context

StackExchange Computer Science Q#95868, answer score: 2

Revisions (0)

No revisions yet.