LeetCode – Max Points on a Line (Java)

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.


This problem can be solve by counting points that have the same slope for each point. When counting, we need to be careful about the duplicate points and points on the vertical lines.

Java Solution

public int maxPoints(Point[] points) {
    if(points == null || points.length == 0) return 0;
    HashMap<Double, Integer> result = new HashMap<Double, Integer>();
    int max=0;
    for(int i=0; i<points.length; i++){
        int duplicate = 1;//
        int vertical = 0;
        for(int j=i+1; j<points.length; j++){
            //handle duplicates and vertical
            if(points[i].x == points[j].x){
                if(points[i].y == points[j].y){
                double slope = points[j].y == points[i].y ? 0.0
				        : (1.0 * (points[j].y - points[i].y))
						/ (points[j].x - points[i].x);
                if(result.get(slope) != null){
                    result.put(slope, result.get(slope) + 1);
                    result.put(slope, 1);
        for(Integer count: result.values()){
            if(count+duplicate > max){
                max = count+duplicate;
        max = Math.max(vertical + duplicate, max);
    return max;

The relation between normal cases, vertical cases and duplicate cases can be shown as follows:

Category >> Algorithms >> Interview >> Java  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
String foo = "bar";

  1. Arnaud Hebert on 2015-5-13

    When there is no match for result.get(slope), you better initialize with 2: result.put(slope, 2);
    There is at least 2 points for each line.

  2. SS on 2015-9-3

    double slope = points[j].y == points[i].y ? 0.0
    : (1.0 * (points[j].y - points[i].y))
    / (points[j].x - points[i].x);

  3. SS on 2015-9-3

    what is the “1.0” for? I forgot this part the it stuck at 20/27 test case.

  4. Vishlesh Patel on 2015-9-25

    I believe time complexity here is O(n^3). Can we reduce it to O(n^2)?

  5. AR on 2016-2-25

    This is clearly wrong. If you are storing the slope as a double you might never get a match for certain cases. We should store it in fractional form of p/q.

  6. Robert Wagner on 2016-2-26

    the coursera course by princeton “Algorithms: I” by Sedgewick has this as its 2nd programming assignment, and the correct solution is O(n^2 lg n) but also outputs a list of vectors of the collinear line segments, not just counting the # of points in a line

  7. Aditya on 2016-5-22

    What happens when two lines are parallel. They have the same slope but are not collinear?

  8. Mukul Jain on 2016-5-29

    This solution is not right. As Aditya and AR pointed out below. Just comparing slop is not enough ..also double as key will not work as well ?

  9. Andre on 2016-6-5

    Valid point.. has anyone found solution to parallel lines case?

  10. R. Wang on 2016-7-30

    Why should the “duplicate” variable be initialized “1” instead of “0”?

  11. בן on 2016-8-2

    This will work also when two lines are parallel. Notice that the hashmap is deleted for each point, therefore saving the slopes is only valid to the current point we are looking at

  12. William Kuo on 2016-9-13

    Because the point itself is counted in duplicate. Otherwise, the point would not be counted in the line based on itself.

  13. Raj on 2017-2-20

    this fails for [[0,0],[94911151,94911150],[94911152,94911151]]

Leave a comment