Count number of pairs of lines intersecting at a Point
Given N lines are in the form a*x + b*y = c (a>0 or a==0 & b>0). Find the number of pairs of lines intersecting at a point.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
x + 2y = 2
x + 2y = 4
- Parallel lines never intersect so a method is needed to exclude parallel lines for each line.
- The slope of a line can be represented as pair(a, b). Construct a map with key as slope and value as a set with c as entries in it so that it has an account of the parallel lines.
- Iterate over the lines add them to the map and maintain a variable Tot which counts the total number of lines till now.
- Now for each line update the Tot variable then add Tot to the answer and subtract the number of parallel lines to that line including itself.
Below is the implementation of the above approach: