Skip to content
Related Articles

Related Articles

Count number of pairs of lines intersecting at a Point

Improve Article
Save Article
  • Last Updated : 09 Sep, 2022
Improve Article
Save Article

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

Input: N=5 x + y = 2 x + y = 4 x = 1 x – y = 2 y = 3 Output: 9 Input: N=2 x + 2y = 2 x + 2y = 4 Output: 0

Approach: 

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

CPP




// C++ implementation to calculate
// pair of intersecting lines
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the number
// of intersecting pair of lines
void numberOfPairs(int a[],int b[],int c[],int N){
 
    int count = 0, Tot = 0;
 
    // Construct a map of slope and
    // corresponding c value
    map<pair<int, int>, set<int> > LineMap;
 
    // iterate over each line
    for (int i = 0; i < N; i++) {
 
        // Slope can be represented
        // as pair(a, b)
        pair<int, int> Slope =
                    make_pair(a[i], b[i]);
 
        // Checking if the line does
        // not already exist
        if (!LineMap[Slope].count(c[i])){
            // maintaining a count
            // of total lines
            Tot++;
            LineMap[Slope].insert(c[i]);
 
            // subtracting the count of
            // parallel lines including itself
            count += Tot -
                    LineMap[Slope].size();
        }
    }
 
    cout << count << endl;
}
 
// Driver code
int main()
{
    // A line can be represented as ax+by=c
    // such that (a>0 || (a==0 & b>0) )
    // a and b are already in there lowest
    // form i.e gcd(a, b)=1
    int N = 5;
    int a[] = { 1, 1, 1, 1, 0 };
    int b[] = { 1, 1, 0, -1, 1 };
    int c[] = { 2, 4, 1, 2, 3 };
     
    numberOfPairs(a,b,c,N);
 
    return 0;
}

Java




// Java implementation to calculate
// pair of intersecting lines
 
import java.util.*;
 
class GFG
{
 
  // Function to return the number
  // of intersecting pair of lines
  static void numberOfPairs(int[] a, int[] b, int[] c,
                            int N)
  {
 
    int count = 0;
    int Tot = 0;
 
    // Construct a map of slope and
    // corresponding c value
    HashMap<String, HashSet<Integer> > LineMap
      = new HashMap<String, HashSet<Integer> >();
 
    // iterate over each line
    for (int i = 0; i < N; i++) {
 
      // Slope can be represented
      // as pair(a, b)
      String Slope = String.valueOf(a[i]) + "#"
        + String.valueOf(b[i]);
 
      // Checking if the line does
      // not already exist
      if (!LineMap.containsKey(Slope)
          || !LineMap.get(Slope).contains(c[i])) {
        // maintaining a count
        // of total lines
        Tot = Tot + 1;
        if (!LineMap.containsKey(Slope)) {
          HashSet<Integer> h1 = new HashSet<Integer>();
          h1.add(c[i]);
          LineMap.put(Slope, h1);
        }
        else {
          HashSet<Integer> h1 = LineMap.get(Slope);
          h1.add(c[i]);
          LineMap.put(Slope, h1);
        }
 
        // subtracting the count of
        // parallel lines including itself
        count = count + Tot - LineMap.get(Slope).size();
      }
    }
 
    System.out.println(count);
  }
 
  // Driver code
 
  public static void main(String[] args)
  {
 
    // A line can be represented as ax+by=c
    // such that (a>0 || (a==0 & b>0) )
    // a and b are already in there lowest
    // form i.e gcd(a, b)=1
    int N = 5;
    int[] a = { 1, 1, 1, 1, 0 };
    int[] b = { 1, 1, 0, -1, 1 };
    int[] c = { 2, 4, 1, 2, 3 };
 
    numberOfPairs(a, b, c, N);
  }
}
 
// The code is contributed by phasing17

Python3




# Python3 implementation to calculate
# pair of intersecting lines
 
# Function to return the number
# of intersecting pair of lines
def numberOfPairs(a, b, c, N):
     
    count = 0;
    Tot = 0;
 
    # Construct a map of slope and
    # corresponding c value
    LineMap = dict()
 
    # iterate over each line
    for i in range(N):
 
        # Slope can be represented
        # as pair(a, b)
        Slope = (a[i], b[i])
 
        # Checking if the line does
        # not already exist
        if Slope not in LineMap or c[i] not in LineMap[Slope]:
            # maintaining a count
            # of total lines
            Tot = Tot + 1;
            if Slope not in LineMap:
                s1 = set()
                s1.add(c[i])
                LineMap[Slope] = s1
            else:
                LineMap[Slope].add(c[i])
 
            # subtracting the count of
            # parallel lines including itself
            count = count +  Tot - len(LineMap[Slope]);
 
 
    print(count);
 
 
# Driver code
# A line can be represented as ax+by=c
# such that (a>0 || (a==0 & b>0) )
# a and b are already in there lowest
# form i.e gcd(a, b)=1
N = 5;
a = [1, 1, 1, 1, 0];
b = [1, 1, 0, -1, 1];
c = [2, 4, 1, 2, 3 ];
 
numberOfPairs(a,b,c,N);
 
# The code is contributed by phasing17

C#




// C# implementation to calculate
// pair of intersecting lines
using System;
using System.Collections.Generic;
 
class GFG
{
 
  // Function to return the number
  // of intersecting pair of lines
  static void numberOfPairs(int[] a, int[] b, int[] c,
                            int N)
  {
 
    int count = 0;
    int Tot = 0;
 
    // Construct a map of slope and
    // corresponding c value
    Dictionary<string, HashSet<int> > LineMap
      = new Dictionary<string, HashSet<int> >();
 
    // iterate over each line
    for (int i = 0; i < N; i++) {
 
      // Slope can be represented
      // as pair(a, b)
      string Slope = Convert.ToString(a[i]) + "#"
        + Convert.ToString(b[i]);
 
      // Checking if the line does
      // not already exist
      if (!LineMap.ContainsKey(Slope)
          || !LineMap[Slope].Contains(c[i])) {
        // maintaining a count
        // of total lines
        Tot = Tot + 1;
        if (!LineMap.ContainsKey(Slope)) {
          HashSet<int> h1 = new HashSet<int>();
          h1.Add(c[i]);
          LineMap[Slope] = h1;
        }
        else {
          HashSet<int> h1 = LineMap[Slope];
          h1.Add(c[i]);
          LineMap[Slope] = h1;
        }
 
        // subtracting the count of
        // parallel lines including itself
        count = count + Tot - LineMap[Slope].Count;
      }
    }
 
    Console.WriteLine(count);
  }
 
  // Driver code
 
  public static void Main(string[] args)
  {
 
    // A line can be represented as ax+by=c
    // such that (a>0 || (a==0 & b>0) )
    // a and b are already in there lowest
    // form i.e gcd(a, b)=1
    int N = 5;
    int[] a = { 1, 1, 1, 1, 0 };
    int[] b = { 1, 1, 0, -1, 1 };
    int[] c = { 2, 4, 1, 2, 3 };
 
    numberOfPairs(a, b, c, N);
  }
}
 
// The code is contributed by phasing17

Javascript




// JavaScript implementation to calculate
// pair of intersecting lines
 
// Function to return the number
// of intersecting pair of lines
function numberOfPairs(a, b, c, N){
 
    let count = 0;
    let Tot = 0;
 
    // Construct a map of slope and
    // corresponding c value
    let LineMap = new Map();
    // map<pair<int, int>, set<int> > LineMap;
 
    // iterate over each line
    for (let i = 0; i < N; i++) {
 
        // Slope can be represented
        // as pair(a, b)
        let Slope = [a[i], b[i]].join();
 
        // Checking if the line does
        // not already exist
        if (!LineMap.has(Slope) || !LineMap.get(Slope).has(c[i])){
            // maintaining a count
            // of total lines
            Tot = Tot + 1;
            if(!LineMap.has(Slope))
                LineMap.set(Slope, new Set().add(c[i]));
            else
                LineMap.set(Slope, LineMap.get(Slope).add(c[i]));
 
            // subtracting the count of
            // parallel lines including itself
            count = count +  Tot - LineMap.get(Slope).size;
        }
    }
 
    console.log(count);
}
 
// Driver code
// A line can be represented as ax+by=c
// such that (a>0 || (a==0 & b>0) )
// a and b are already in there lowest
// form i.e gcd(a, b)=1
let N = 5;
let a = [1, 1, 1, 1, 0];
let b = [1, 1, 0, -1, 1];
let c = [2, 4, 1, 2, 3 ];
 
numberOfPairs(a,b,c,N);
 
// The code is contributed by Gatuam goel (gautamgoel962)

Output:

9

Time Complexity: O(N*log(N))

Space Complexity: O(N) since using a map


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!