Program to count number of points that lie on a line in Python

Given a list of coordinates representing points on a Cartesian plane, we need to find the maximum number of points that lie on a single line. This problem requires calculating slopes between points and grouping them accordingly.

So, if the input is like coordinates = [[6, 2],[8, 3],[10, 4],[1, 1],[2, 2],[6, 6],[7, 7]], then the output will be 4, as the points [1, 1], [2, 2], [6, 6], [7, 7] lie on the same line with slope 1.

Algorithm

To solve this problem, we follow these steps ?

  • Initialize result counter to 0

  • For each point as reference point:

    • Create a dictionary to store slope frequencies

    • Count duplicate points at the same location

    • For each other point, calculate the slope:

      • If x-coordinates are equal, use infinity as slope (vertical line)

      • If points are identical, increment duplicate counter

      • Otherwise, calculate slope = (y2 - y1) / (x2 - x1)

    • Update result with maximum points on any line through current reference point

Implementation

class Solution:
    def solve(self, points):
        if len(points) <= 2:
            return len(points)
        
        res = 0
        for i in range(len(points)):
            x1, y1 = points[i][0], points[i][1]
            slopes = {}
            same = 1  # Count current point
            
            for j in range(i + 1, len(points)):
                x2, y2 = points[j][0], points[j][1]
                
                if x2 == x1:  # Vertical line
                    slopes[float("inf")] = slopes.get(float("inf"), 0) + 1
                elif x1 == x2 and y1 == y2:  # Same point
                    same += 1
                else:  # Regular slope
                    slope = (y2 - y1) / (x2 - x1)
                    slopes[slope] = slopes.get(slope, 0) + 1
            
            # Calculate max points on any line through current point
            if slopes:
                res = max(res, same + max(slopes.values()))
            else:
                res = max(res, same)  # Only duplicate points
        
        return res

# Test the solution
ob = Solution()
coordinates = [[6, 2],[8, 3],[10, 4],[1, 1],[2, 2],[6, 6],[7, 7]]
print("Maximum points on a line:", ob.solve(coordinates))
Maximum points on a line: 4

How It Works

The algorithm works by considering each point as a reference and calculating slopes to all other points. Points with the same slope from the reference point lie on the same line. Special cases include:

  • Vertical lines: When x-coordinates are equal, we use infinity as the slope

  • Duplicate points: Multiple points at the same coordinates are counted separately

  • Edge cases: Handle lists with 0, 1, or 2 points directly

Example Walkthrough

For the input [[6, 2],[8, 3],[10, 4],[1, 1],[2, 2],[6, 6],[7, 7]]:

  • Points [1, 1], [2, 2], [6, 6], [7, 7] have slope = 1

  • Points [6, 2], [8, 3], [10, 4] have slope = 0.5

  • The maximum count is 4 points on the line with slope 1

Time Complexity

The time complexity is O(n²) where n is the number of points, as we check each pair of points once. The space complexity is O(n) for storing slopes in the dictionary.

Conclusion

This solution efficiently finds the maximum number of collinear points by calculating slopes between point pairs. The key insight is handling special cases like vertical lines and duplicate points correctly.

Updated on: 2026-03-25T13:49:16+05:30

754 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements