# Max Points on a Line – LeetCode Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.

Given an array of `points`

where `points[i] = [x`

represents a point on the _{i}, y_{i}]**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`

`-10`

^{4}<= x_{i}, y_{i}<= 10^{4}- All the
`points`

are**unique**.

# C++ Max Points on a Line LeetCode Solution

```
``````
class Solution {
public:
int maxPoints(vector<Point>& points) {
int n = points.size(), ans = 0;
for (int i = 0; i < n; i++) {
unordered_map<string, int> counter;
int dup = 1;
for (int j = i + 1; j < n; j++) {
if (points[j].x == points[i].x && points[j].y == points[i].y) {
dup++;
} else {
int dx = points[j].x - points[i].x, dy = points[j].y - points[i].y, g = gcd(dx, dy);
counter[to_string(dx / g) + '_' + to_string(dy / g)]++;
}
}
ans = max(ans, dup);
for (auto p : counter) {
ans = max(ans, p.second + dup);
}
}
return ans;
}
private:
int gcd(int a, int b) {
while (b) {
a = a % b;
swap(a, b);
}
return a;
}
};
```

# Java Max Points on a Line LeetCode Solution

```
``````
public class Solution{
public int maxPoints(Point[] points) {
if (points==null) return 0;
if (points.length<=2) return points.length;
Map<Integer,Map<Integer,Integer>> map = new HashMap<Integer,Map<Integer,Integer>>();
int result=0;
for (int i=0;i<points.length;i++){
map.clear();
int overlap=0,max=0;
for (int j=i+1;j<points.length;j++){
int x=points[j].x-points[i].x;
int y=points[j].y-points[i].y;
if (x==0&&y==0){
overlap++;
continue;
}
int gcd=generateGCD(x,y);
if (gcd!=0){
x/=gcd;
y/=gcd;
}
if (map.containsKey(x)){
if (map.get(x).containsKey(y)){
map.get(x).put(y, map.get(x).get(y)+1);
}else{
map.get(x).put(y, 1);
}
}else{
Map<Integer,Integer> m = new HashMap<Integer,Integer>();
m.put(y, 1);
map.put(x, m);
}
max=Math.max(max, map.get(x).get(y));
}
result=Math.max(result, max+overlap+1);
}
return result;
}
private int generateGCD(int a,int b){
if (b==0) return a;
else return generateGCD(b,a%b);
}
}
```

# Python 3 Max Points on a Line LeetCode Solution

```
``````
def maxPoints(self, points):
l = len(points)
m = 0
for i in range(l):
dic = {'i': 1}
same = 0
for j in range(i+1, l):
tx, ty = points[j].x, points[j].y
if tx == points[i].x and ty == points[i].y:
same += 1
continue
if points[i].x == tx: slope = 'i'
else:slope = (points[i].y-ty) * 1.0 /(points[i].x-tx)
if slope not in dic: dic[slope] = 1
dic[slope] += 1
m = max(m, max(dic.values()) + same)
return m
```

Array-1180

String-562

Hash Table-412

Dynamic Programming-390

Math-368

Sorting-264

Greedy-257

Depth-First Search-256

Database-215

Breadth-First Search-200

Tree-195

Binary Search-191

Matrix-176

Binary Tree-160

Two Pointers-151

Bit Manipulation-140

Stack-133

Heap (Priority Queue)-117

Design-116

Graph-108

Simulation-103

Prefix Sum-96

Backtracking-92

Counting-86

Sliding Window-73

Linked List-69

Union Find-66

Ordered Set-48

Monotonic Stack-47

Recursion-43

Trie-41

Binary Search Tree-40

Divide and Conquer-40

Enumeration-39

Bitmask-37

Queue-33

Memoization-32

Topological Sort-31

Geometry-30

Segment Tree-27

Game Theory-24

Hash Function-24

Binary Indexed Tree-21

Interactive-18

Data Stream-17

String Matching-17

Rolling Hash-17

Shortest Path-16

Number Theory-16

Combinatorics-15

Randomized-12

Monotonic Queue-9

Iterator-9

Merge Sort-9

Concurrency-9

Doubly-Linked List-8

Brainteaser-8

Probability and Statistics-7

Quickselect-7

Bucket Sort-6

Suffix Array-6

Minimum Spanning Tree-5

Counting Sort-5

Shell-4

Line Sweep-4

Reservoir Sampling-4

Eulerian Circuit-3

Radix Sort-3

Strongly Connected Componen-t2

Rejection Sampling-2

Biconnected Component-1

## Leave a comment below