Skip to content
Related Articles
Open in App
Not now

Related Articles

Count number of intersections points for given lines between (i, 0) and (j, 1)

Improve Article
Save Article
  • Last Updated : 04 Dec, 2021
Improve Article
Save Article

Given an array, lines[] of N pairs of the form (i, j) where (i, j) represents a line segment from coordinate (i, 0) to (j, 1), the task is to find the count of points of intersection of the given lines.


Input: lines[] = {{1, 2}, {2, 1}}
Output: 1
Explanation: For the given two pairs, the line form (1, 0) to (2, 1) intersect with the line from (2, 0) to (1, 1) at point (1.5, 0.5). Hence the total count of points of intersection is 1.

Input: lines[] = {{1, 5}, {2, 1}, {3, 7}, {4, 1}, {8, 2}}
Output: 5


Approach: The given problem can be solved using a Greedy approach using the policy-based data structure. It can be observed that for lines represented b two pairs (a, b) and (c, d) to intersect either (a > c and b < d) or (a < c and b > d) must hold true. 

Therefore using this observation, the given array of pairs can be sorted in decreasing order of the 1st element. While traversing the array, insert the value of the second element into the policy-based data structure and find the count of elements smaller than the second element of the inserted pair using the order_of_key function and maintain the sum of count in a variable. Similarly, calculate for the cases after sorting the given array of pairs in decreasing order of their 2nd element.

Below is the implementation of the above approach:


// C++ Program of the above approach
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
// Defining Policy Based Data Structure
typedef tree<int, null_type,
             less_equal<int>, rb_tree_tag,
// Function to count points
// of intersection of pairs
// (a, b) and (c, d)
// such that a > c and b < d
int cntIntersections(
    vector<pair<int, int> > lines,
    int N)
    // Stores the count
    // of intersection points
    int cnt = 0;
    // Initializing Ordered Multiset
    ordered_multiset s;
    // Loop to iterate the array
    for (int i = 0; i < N; i++) {
        // Add the count of integers
        // smaller than lines[i].second
        // in the total count
        cnt += s.order_of_key(lines[i].second);
        // Insert lines[i].second into s
    // Return Count
    return cnt;
// Function to find the
// total count of points of
// intersections of all the given lines
int cntAllIntersections(
    vector<pair<int, int> > lines,
    int N)
    // Sort the array in decreasing
    // order of 1st element
    sort(lines.begin(), lines.end(),
         greater<pair<int, int> >());
    // Stores the total count
    int totalCnt = 0;
    // Function call for cases
    // with a > c and b < d
    totalCnt += cntIntersections(lines, N);
    // Swap all the pairs of the array in order
    // to calculate cases with a < c and b > d
    for (int i = 0; i < N; i++) {
        swap(lines[i].first, lines[i].second);
    // Function call for cases
    // with a < c and b > d
    totalCnt += cntIntersections(lines, N);
    // Return Answer
    return totalCnt;
// Driver Code
int main()
    vector<pair<int, int> > lines{
        {1, 5}, {2, 1}, {3, 7}, {4, 1}, {8, 2}
    cout << cntAllIntersections(lines,
    return 0;




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

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!