> For the complete documentation index, see [llms.txt](https://ugopireddy.gitbook.io/leet-code-solutions/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ugopireddy.gitbook.io/leet-code-solutions/solutions_101_-_150/149-max-points-on-a-line.md).

# 149 Max Points on a Line

### Problem

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

### Solutions

```java
public class Solution {
    public int maxPoints(Point[] points) {
        int res = 0;
        for (int i = 0; i < points.length; ++i) {
            HashMap<Integer, HashMap<Integer, Integer>> lines = new HashMap<>();
            int duplicate = 1;
            for (int j = i + 1; j < points.length; ++j) {
                if (points[i].x == points[j].x && points[i].y == points[j].y) {
                    duplicate++; 
                    continue;
                }
                int dx = points[j].x - points[i].x;
                int dy = points[j].y - points[i].y;
                int d = gcd(dx, dy);
                if (!lines.containsKey(dx/d)) {
                    lines.put(dx/d, new HashMap<Integer, Integer>());
                }
                if (!lines.get(dx/d).containsKey(dy/d)) {
                    lines.get(dx/d).put(dy/d, 0);
                }
                lines.get(dx/d).put(dy/d, lines.get(dx/d).get(dy/d) + 1);
            }
            res = Math.max(res, duplicate);
            for (Integer a:lines.keySet()) {
                for (Integer b:lines.get(a).keySet()) {
                    res = Math.max(res, lines.get(a).get(b) + duplicate);
                }
            }
        }
        return res;
    }
    // greatest common divisor
    public int gcd(int a, int b) {
        return (b == 0) ? a : gcd(b, a % b);
    }
}
```
