Maximum Points on a Line

Asked atAmazon
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below

The problem

Given an array of unqiue points nums, where each point is represented as nums[i] = [xi, yi] on a 2D plane, find the maximum number of points that lie on the same line.

Input: nums = [[0,0], [1,1], [2,2], [3,3]] Output: 4 Explanation: All four points lie on the line with slope 1, so the maximum number of points on the same line is 4.

Input: nums = [[0,1], [2,3], [4,5], [1,2], [3,4], [2,2]] Output: 5 Explanation: The points [0,1], [1,2], [2,3], [3,4], and [4,5] all lie on the same straight line, so the maximum number is 5.

Input: nums = [[2,3], [3,4], [5,1], [7,2], [6,5], [9,4]]

  • 1 <= nums.length <= 500
  • nums[i].length == 2
  • -104 <= xi, yi <= 104

cpp

class Solution {
  public: 
  int maximumPointsOnALine(vector<vector<int>> & nums) {
   
  }
};

java

class Solution {
    public int maximumPointsOnALine(int[][] nums) {
       
    }
    
}

python

class Solution:
    def maximumPointsOnALine(self, nums):

javascript

class Solution {
    maximumPointsOnALine(nums) {
        
    }
}

csharp

public class Solution
{
    public int MaximumPointsOnALine(int[][] nums)
    {
       
    }

}

go

func maximumPointsOnALine(nums [][]int) int
Solve on LeetCode →
Stuck? Show a way to structure it+
  1. 01Choose each point as an anchor
  2. 02Count normalized slopes to later points
  3. 03Reduce delta pairs by gcd
  4. 04Canonicalize signs and vertical or horizontal lines
  5. 05Add duplicates and track the largest bucket

Reference answer

Then expect these follow-ups

  • How do you normalize (-2,-4) and (1,2)?

    Tests: follow-up reasoning

  • What changes if coordinates exceed 32-bit range?

    Tests: constraint adaptation

Free to read · better with Enzo

Practice this out loud with Enzo

Enzo runs it as a mock interview, pushes back with follow-ups, and grades you on the rubric.

Next question