This problem has a simple geometric interpretation, which shows that it can be solved O (n ^ 2) times and probably cannot be solved faster (short for 3SUM ). Suppose our array is [1, 2, 10, 3, 5] . We can write this array as a sequence of points
(0,1), (1,2), (2,10), (3,3), (4,5)
in which the value x is the index of the array element, and the y-value is the value of the array element. Now the question is to find a line that conveys the maximum possible number of points in this set. The cost of transforming an array is the number of points that are not on the line, which is minimized when the maximum number of points on the line.
A decisive answer to this question is given in this SO publication: What is the most efficient algorithm for finding a straight line through most points?
Idea: for each point P in the set from left to right, find the line passing through this point and the maximum number of points to the right of P. (We do not need to look at the points to the left of P, because they would be caught at an earlier iteration).
To find the maximum number of P-collinear points to the right of P, for each such point Q we calculate the slope of the segment PQ. Focus the various slopes on the hash map. The slope that displays the maximum number of hits is what you are looking for.
Technical issue: You probably don't want to use floating point arithmetic to calculate slopes. On the other hand, if you use rational numbers, you will probably have to calculate the greatest common factor to compare fractions by comparing the numerator and denominator, which multiplies the operating time by the coefficient log n. Instead, you should verify that the rational numbers a/b and c/d equal by checking ad == bc .
The SO link, the link to which is given above, gives an abbreviation for 3SUM , i.e. this problem is 3SUM-hard, which shows that if this problem can be solved much faster than O (n ^ 2), then 3SUM can also be solved much faster than O (n ^ 2). Here a condition occurs, which includes integers (-inf, inf). If it is known that integers are taken from a limited set, the abbreviation from 3SUM is not final.
An interesting question is whether the idea on Wikipedia can solve 3SUM in O (n + N log N) time, when integers are in a limited set (-N, N), can be used to solve the minimum cost to convert an array to an AP problem in time faster than O (n ^ 2).