Practice › Hashing & Two Pointers
Max Points on a Line
Hard · Hashing & Two Pointers · Asked at Google, Amazon, Apple, LinkedIn
Given n distinct points with integer coordinates, print the largest number of them that lie on one straight line.
Input
First line: n. Next n lines: x y.
Output
The maximum number of points on one line.
Constraints
- 1 ≤ n ≤ 300
- −10^4 ≤ x, y ≤ 10^4
- all points distinct
Fix each point as an anchor and group the other points by the direction to them. Store a direction as (dx, dy) divided by gcd(|dx|, |dy|) with a fixed sign, so equal slopes give equal keys without floating point. The biggest group + 1 is a candidate.
Example 1
Input
3 1 1 2 2 3 3
Output
3
Example 2
Input
6 1 1 3 2 5 3 4 1 2 3 1 4
Output
4
Hints
- Every line through two or more points passes through some point as an anchor.
- From an anchor, points on the same line have the same direction.
- Represent a direction exactly as a reduced fraction (dx, dy) — not a floating-point slope.
Approach
For each anchor i, look at every later point j and compute dx, dy. Divide both by g = gcd(|dx|, |dy|) and fix the sign (dx > 0, or dx = 0 and dy > 0) so opposite directions give the same key. Count keys in a hash map; the largest count + 1 (the anchor) is a candidate. Pairs with an earlier anchor were already counted from that anchor, so only later points are needed.
Complexity: O(n² log C) time · O(n) space
Solution
C++ solution
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<long long> x(n), y(n);
for (int i = 0; i < n; i++) cin >> x[i] >> y[i];
if (n <= 2) {
cout << n << "\n";
return 0;
}
int best = 0;
for (int i = 0; i < n; i++) {
map<pair<long long, long long>, int> cnt;
for (int j = i + 1; j < n; j++) {
long long dx = x[j] - x[i], dy = y[j] - y[i];
long long g = __gcd(llabs(dx), llabs(dy));
dx /= g;
dy /= g;
if (dx < 0 || (dx == 0 && dy < 0)) {
dx = -dx;
dy = -dy;
}
best = max(best, ++cnt[{dx, dy}] + 1);
}
}
cout << best << "\n";
return 0;
}Python solution
import sys
from math import gcd
data = list(map(int, sys.stdin.read().split()))
n = data[0]
pts = [(data[1 + 2 * i], data[2 + 2 * i]) for i in range(n)]
if n <= 2:
print(n)
else:
best = 0
for i in range(n):
cnt = {}
xi, yi = pts[i]
for j in range(i + 1, n):
dx, dy = pts[j][0] - xi, pts[j][1] - yi
g = gcd(dx, dy)
dx //= g
dy //= g
if dx < 0 or (dx == 0 and dy < 0):
dx, dy = -dx, -dy
c = cnt.get((dx, dy), 0) + 1
cnt[(dx, dy)] = c
if c + 1 > best:
best = c + 1
print(best)Java solution
import java.io.*;
import java.util.*;
public class Main {
static long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
public static void main(String[] args) throws IOException {
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
in.nextToken();
int n = (int) in.nval;
long[] x = new long[n], y = new long[n];
for (int i = 0; i < n; i++) {
in.nextToken();
x[i] = (long) in.nval;
in.nextToken();
y[i] = (long) in.nval;
}
if (n <= 2) {
System.out.println(n);
return;
}
int best = 0;
for (int i = 0; i < n; i++) {
HashMap<Long, Integer> cnt = new HashMap<>();
for (int j = i + 1; j < n; j++) {
long dx = x[j] - x[i], dy = y[j] - y[i];
long g = gcd(Math.abs(dx), Math.abs(dy));
dx /= g;
dy /= g;
if (dx < 0 || (dx == 0 && dy < 0)) {
dx = -dx;
dy = -dy;
}
long key = dx * 100000L + dy; // |dx|, |dy| ≤ 2 · 10^4
best = Math.max(best, cnt.merge(key, 1, Integer::sum) + 1);
}
}
System.out.println(best);
}
}