# Maximum distinct lines passing through a single point

Given lines represented by two points and . The task is to find maximum number of lines which can pass through a single point, without superimposing (or covering) any other line. We can move any line but not rotate it.

**Examples:**

Input : Line 1 : x1 = 1, y1 = 1, x2 = 2, y2 = 2 Line 2 : x2 = 2, y1 = 2, x2 = 4, y2 = 10 Output : 2 There are two lines. These two lines are not parallel, so both of them will pass through a single point. Input : Line 1 : x1 = 1, y1 = 5, x2 = 1, y2 = 10 Line 2 : x2 = 5, y1 = 1, x2 = 10, y2 = 1 Output : 2

- Represent lines as pair where line can be given as , called line slope form. We can now see that we can change the
**c**for any line, but cannot modify**m**. - Lines having same value of
**m**parallel, given that**(c1 ≠ c2)**. Also no two parallel lines can pass through same point without superimposing to each other. - So, our problem reduces to finding different values of slopes from given set of lines.

We can calculate slope of a line as , add them to a set and count the number of distinct values of slope in set. But we have to handle vertical lines separately.

So, if then, **slope = INT_MAX**.

Otherwise, **slope = **.

Below is the implementation of the approach.

## C++

`// C++ program to find maximum number of lines ` `// which can pass through a single point ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to find maximum lines which passes ` `// through a single point ` `int` `maxLines(` `int` `n, ` `int` `x1[], ` `int` `y1[], ` ` ` `int` `x2[], ` `int` `y2[]) ` `{ ` ` ` `unordered_set<` `double` `> s; ` ` ` ` ` `double` `slope; ` ` ` `for` `(` `int` `i = 0; i < n; ++i) { ` ` ` `if` `(x1[i] == x2[i]) ` ` ` `slope = INT_MAX; ` ` ` `else` ` ` `slope = (y2[i] - y1[i]) * 1.0 ` ` ` `/ (x2[i] - x1[i]) * 1.0; ` ` ` ` ` `s.insert(slope); ` ` ` `} ` ` ` ` ` `return` `s.size(); ` `} ` ` ` `// Driver program ` `int` `main() ` `{ ` ` ` `int` `n = 2, x1[] = { 1, 2 }, y1[] = { 1, 2 }, ` ` ` `x2[] = { 2, 4 }, y2[] = { 2, 10 }; ` ` ` `cout << maxLines(n, x1, y1, x2, y2); ` ` ` `return` `0; ` `} ` `// This code is written by ` `// Sanjit_Prasad ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find maximum number of lines ` `// which can pass through a single point ` ` ` `import` `java.util.*; ` `import` `java.lang.*; ` `import` `java.io.*; ` ` ` `class` `GFG{ ` ` ` `// function to find maximum lines which passes ` `// through a single point ` `static` `int` `maxLines(` `int` `n, ` `int` `x1[], ` `int` `y1[], ` ` ` `int` `x2[], ` `int` `y2[]) ` `{ ` ` ` `Set<Double> s=` `new` `HashSet<Double>(); ` ` ` ` ` `double` `slope; ` ` ` `for` `(` `int` `i = ` `0` `; i < n; ++i) { ` ` ` `if` `(x1[i] == x2[i]) ` ` ` `slope = Integer.MAX_VALUE; ` ` ` `else` ` ` `slope = (y2[i] - y1[i]) * ` `1.0` ` ` `/ (x2[i] - x1[i]) * ` `1.0` `; ` ` ` ` ` `s.add(slope); ` ` ` `} ` ` ` ` ` `return` `s.size(); ` `} ` ` ` `// Driver program ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `n = ` `2` `, x1[] = { ` `1` `, ` `2` `}, y1[] = { ` `1` `, ` `2` `}, ` ` ` `x2[] = { ` `2` `, ` `4` `}, y2[] = { ` `2` `, ` `10` `}; ` ` ` `System.out.print(maxLines(n, x1, y1, x2, y2)); ` `} ` `} ` `// This code is written by ` `// Subhadeep ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find maximum number ` `# of lines which can pass through a ` `# single point ` `import` `sys ` `# function to find maximum lines ` `# which passes through a single point ` `def` `maxLines(n, x1, y1, x2, y2): ` ` ` ` ` `s ` `=` `[]; ` ` ` ` ` `slope` `=` `sys.maxsize; ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `if` `(x1[i] ` `=` `=` `x2[i]): ` ` ` `slope ` `=` `sys.maxsize; ` ` ` `else` `: ` ` ` `slope ` `=` `(y2[i] ` `-` `y1[i]) ` `*` `1.0` `/` `(x2[i] ` `-` `x1[i]) ` `*` `1.0` `; ` ` ` ` ` `s.append(slope); ` ` ` ` ` `return` `len` `(s); ` ` ` `# Driver Code ` `n ` `=` `2` `; ` `x1 ` `=` `[ ` `1` `, ` `2` `]; ` `y1 ` `=` `[` `1` `, ` `2` `]; ` `x2 ` `=` `[` `2` `, ` `4` `]; ` `y2 ` `=` `[` `2` `, ` `10` `]; ` `print` `(maxLines(n, x1, y1, x2, y2)); ` ` ` `# This code is contributed by mits ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find maximum number of lines ` `// which can pass through a single point ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG ` `{ ` ` ` `// function to find maximum lines which passes ` `// through a single point ` `static` `int` `maxLines(` `int` `n, ` `int` `[]x1, ` `int` `[]y1, ` ` ` `int` `[]x2, ` `int` `[]y2) ` `{ ` ` ` `HashSet<Double> s = ` `new` `HashSet<Double>(); ` ` ` ` ` `double` `slope; ` ` ` `for` `(` `int` `i = 0; i < n; ++i) ` ` ` `{ ` ` ` `if` `(x1[i] == x2[i]) ` ` ` `slope = ` `int` `.MaxValue; ` ` ` `else` ` ` `slope = (y2[i] - y1[i]) * 1.0 ` ` ` `/ (x2[i] - x1[i]) * 1.0; ` ` ` ` ` `s.Add(slope); ` ` ` `} ` ` ` ` ` `return` `s.Count; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `n = 2; ` ` ` `int` `[]x1 = { 1, 2 }; ` `int` `[]y1 = { 1, 2 }; ` ` ` `int` `[]x2 = { 2, 4 }; ` `int` `[]y2 = { 2, 10 }; ` ` ` `Console.Write(maxLines(n, x1, y1, x2, y2)); ` `} ` `} ` ` ` `/* This code contributed by PrinciRaj1992 */` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find maximum number ` `// of lines which can pass through a ` `// single point ` ` ` `// function to find maximum lines ` `// which passes through a single point ` `function` `maxLines(` `$n` `, ` `$x1` `, ` `$y1` `, ` `$x2` `, ` `$y2` `) ` `{ ` ` ` `$s` `= ` `array` `(); ` ` ` ` ` `$slope` `; ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ++` `$i` `) ` ` ` `{ ` ` ` `if` `(` `$x1` `[` `$i` `] == ` `$x2` `[` `$i` `]) ` ` ` `$slope` `= PHP_INT_MAX; ` ` ` `else` ` ` `$slope` `= (` `$y2` `[` `$i` `] - ` `$y1` `[` `$i` `]) * 1.0 / ` ` ` `(` `$x2` `[` `$i` `] - ` `$x1` `[` `$i` `]) * 1.0; ` ` ` ` ` `array_push` `(` `$s` `, ` `$slope` `); ` ` ` `} ` ` ` ` ` `return` `count` `(` `$s` `); ` `} ` ` ` `// Driver Code ` `$n` `= 2; ` `$x1` `= ` `array` `( 1, 2 ); ` `$y1` `= ` `array` `(1, 2); ` `$x2` `= ` `array` `(2, 4); ` `$y2` `= ` `array` `(2, 10); ` `echo` `maxLines(` `$n` `, ` `$x1` `, ` `$y1` `, ` `$x2` `, ` `$y2` `); ` ` ` `// This code is contributed by mits ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

2

**Time Complexity:**

## Recommended Posts:

- Program for Point of Intersection of Two Lines
- Find intersection point of lines inside a section
- Integers from the range that are composed of a single distinct digit
- Equation of straight line passing through a given point which bisects it into two equal line segments
- Maximum points of intersection n lines
- Maximum of sum and product of digits until number is reduced to a single digit
- Maximum sum of distinct numbers with LCM as N
- Reflection of a point at 180 degree rotation of another point
- Maximum sum of distinct numbers such that LCM of these numbers is N
- Program to find line passing through 2 Points
- Rotation of a point about another point in C++
- Program to find equation of a plane passing through 3 points
- Distance between two parallel lines
- Check whether two straight lines are orthogonal or not
- Check if three straight lines are concurrent or not

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.