VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimum-lines-cover-points/

⇱ Minimum lines to cover all points - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Minimum lines to cover all points

Last Updated : 24 Mar, 2023

Given N points in 2-dimensional space, we need to print the count of the minimum number of lines which traverse through all these N points and which go through a specific (xO, yO) point also.
Examples: 
 

If given points are (-1, 3), (4, 3), (2, 1), (-1, -2), 
(3, -3) and (xO, yO) point is (1, 0) i.e. every line
must go through this point. 
Then we have to draw at least two lines to cover all
these points going through (xO, yO) as shown in below
diagram.


 


We can solve this problem by considering the slope of all points with (xO, yO). If two distinct points have the same slope with (xO, yO) then they can be covered with same line only so we can track slope of each point and whenever we get a new slope we will increase our line count by one. 
In below code slope is stored as a pair of integer to get rid of the precision problem and a set is used to keep track of occurred slopes. 
Please see below code for better understanding. 
 

Output: 
 

2

Time Complexity: O(N) 
Auxiliary Space: O(N)


 

Comment
Article Tags:
Article Tags: