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.
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:
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.