Skip to main content

Max Points on a Line - Solution & Explanation

HardArrayHash TableMathGeometry10 min readAsked at: Amazon, Microsoft, Apple +13
Practice this problem

Problem Statement

Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane, return the maximum number of points that lie on the same straight line.

 

Example 1:

Input: points = [[1,1],[2,2],[3,3]]
Output: 3

Example 2:

Input: points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]
Output: 4

 

Constraints:

  • 1 <= points.length <= 300
  • points[i].length == 2
  • -104 <= xi, yi <= 104
  • All the points are unique.

Approach Overview

Problem Overview: You are given an array of 2D points. The task is to determine the maximum number of points that lie on the same straight line. Any two points define a line, but multiple points can share the same slope relative to an anchor point, indicating collinearity.

Approach 1: Using Slope Calculation with Hashmap (Time: O(n^2), Space: O(n))

Fix one point as an anchor and compute the slope between that point and every other point. If multiple points share the same slope relative to the anchor, they lie on the same line. Store slope frequencies in a hash map where the key represents the slope (typically normalized as a reduced fraction using GCD to avoid floating‑point precision issues). Each iteration resets the map for a new anchor point. Track duplicates separately because identical coordinates create multiple overlapping points on the same line. This approach relies heavily on fast hash lookups and careful slope normalization. It works well because any line through the anchor is uniquely identified by its slope. Problems involving coordinate pairs often combine array traversal with slope hashing techniques from math and hash table design.

Approach 2: Using Line Equation with Hashmap (Time: O(n^2), Space: O(n^2))

Instead of anchoring one point and counting slopes, compute the full line equation formed by every pair of points. A line in 2D can be expressed as Ax + By + C = 0. Normalize the coefficients using the greatest common divisor so equivalent lines map to the same representation. Store each line equation as a key in a hash map and track the set or count of points that lie on it. Because every pair generates a line entry, the map may grow larger than the slope-based method. This method is more explicit from a geometry standpoint and avoids vertical line edge cases, but requires careful normalization to ensure identical lines produce identical keys. It highlights core ideas from computational geometry.

Recommended for interviews: The slope-with-hashmap approach is what most interviewers expect. It demonstrates understanding of geometric properties, hashing, and edge cases such as vertical lines, horizontal lines, and duplicate points. The line equation method shows deeper geometry knowledge but is less common due to higher space overhead. Starting with the slope-based solution and discussing normalization and duplicates usually signals strong problem‑solving ability.

Approach 1: Approach 1: Using Slope Calculation with Hashmap

In this approach, for each point, we will calculate the slope it forms with every other point and store the slope in a hashmap. The number of points with the same slope from a given point can determine the number of points on the same line. We need to be careful with slope representation by using a reduced form using GCD to handle precision issues.

The solution iterates over each point and calculates the slope with every other point, using a hashmap to track and count each slope. The greatest count of any slope originating from a point plus one (for the point itself) gives the maximum number of points on a line through that point.

Code

Python

C++

Java

Complexity

Time Complexity: O(n^2), where n is the number of points. This is because we check each pair of points which leads to n * (n-1)/2 slope calculations.
Space Complexity: O(n), for storing the slopes in the hashmap.

Try this approach in the editor →

Approach 2: Approach 2: Using Line Equation with Hashmap

Alternatively, instead of using slopes directly, we express a line using its coefficients in the line equation format ax + by + c = 0, using GCD to ensure uniqueness in line parameters. This approach confirms lines uniquely, facilitating easier comparisons.

This implementation utilizes line equations represented in ax + by + c form, normalized using gcd, to track lines passing through each point. This avoids precision issues inherent with float slopes.

Code

Python

C++

Complexity

Time Complexity: O(n^2), from comparing each pair of points.
Space Complexity: O(n), for storing normalized line equations.

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Java

C++

Go

C#

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Approach 1: Using Slope Calculation with Hashmap

Time Complexity: O(n^2), where n is the number of points. This is because we check each pair of points which leads to n * (n-1)/2 slope calculations.
Space Complexity: O(n), for storing the slopes in the hashmap.

Approach 2: Using Line Equation with Hashmap

Time Complexity: O(n^2), from comparing each pair of points.
Space Complexity: O(n), for storing normalized line equations.

Default Approach—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Slope Calculation with HashmapO(n^2)O(n)Standard interview solution; efficient counting of collinear points from an anchor point
Line Equation with HashmapO(n^2)O(n^2)When explicitly modeling geometric lines or avoiding slope edge cases like vertical lines

Video Solution

Leetcode 149 - Maximum Points on a Line - Python • NeetCodeIO • 34,128 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Max Points on a Line easy or hard?
Max Points on a Line is classified as a hard problem because it combines geometry, hashing, and careful edge case handling. Challenges include slope normalization, vertical lines, duplicate points, and avoiding floating‑point precision errors.
Max Points on a Line Python/Java solution
Typical Python and Java solutions iterate through points, compute normalized slopes using GCD to avoid floating point errors, and store counts in a hash map. The maximum slope frequency plus duplicate points determines the number of collinear points.
How to solve Max Points on a Line in O(n)?
Solving the problem in O(n) time is not possible in the general case because determining collinearity requires comparing pairs of points. The best known approach is O(n^2) using slope hashing or line normalization.
What is the best approach for Max Points on a Line?
The most practical approach fixes each point as an anchor and counts slopes to every other point using a hash map. Points with the same normalized slope relative to the anchor lie on the same line. This solution runs in O(n^2) time and O(n) space and is the most common interview implementation.
Is Max Points on a Line asked at Google/Amazon/Meta?
Max Points on a Line appears in interviews at companies that emphasize geometry and hashing techniques, including Google, Amazon, and Meta. It tests slope normalization, hash map usage, and careful handling of duplicates and vertical lines.
What data structure is used in Max Points on a Line?
A hash map is the primary data structure. It stores slope counts for each anchor point or normalized line equations when using the line representation method.
What is the time complexity of Max Points on a Line?
The optimal solution runs in O(n^2) time because each point is compared with every other point to compute slopes. For each anchor point, a hash map tracks slope frequencies in O(n) space.

Ready to solve this problem?

Practice Max Points on a Line with our built-in code editor and test cases.

Practice on FleetCode