Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. For example, consider the following points. The maximum number of points over the same straight line is 4. Those points are (1,1), (2,2), (3,3), and (4,4).

Input. The first line consists of an integer, n, that indicates the number of points you will be given. Each of the following n lines consists of a pair of integers separated by one or more spaces corresponding to the x and y components of a point. Consider the following example.

Output.

The output consists of a single number, the maximum number of points that lie on the same straight line. For the example input, the maximum is 4.

# Solution

Here is a Ruby implementation.

# Want to read more?

I love to explain and answer questions on programming problems. I publish a new programming problem and its solution every month. Did I mention that I love to answer questions?

If you would like to get the latest problem + solution, subscribe to the newsletter or subscribe via RSS. You can also follow me on Twitter, GitHub, and LinkedIn.