Number of intersections between two ranges

Given N ranges of type1 ranges and M ranges of type2.The task is to find the total number of intersections between all possible type1 and type2 range pairs. All start and end points of type1 and type2 ranges are given.

Examples:

Input : N = 2, M = 2
type1[ ] = { { 1, 3 }, { 5, 9 } }
type2[ ] = { { 2, 8 }, { 9, 12 } }
Output : 3
Range {2, 8} intersects with type1 ranges {1, 3} and {5, 9}
Range {9, 12} intersects with {5, 9} only.
So the total number of intersections is 3.



Input : N = 3, M = 1
type1[ ] = { { 1, 8 }, { 5, 10 }, { 14, 28 }
type2[ ] = { { 2, 8 } }
Output : 2

Approach:

  • Idea is to use inclusion-exclusion method to determine the total number of intersections.
  • Total possible number of intersections are n * m. Now subtract those count of type1 ranges which do not intersect with ith type2 range.
  • Those type1 ranges will not intersect with ith type2 range which ends before starts of ith type2 range and starts after the end of ith type2 range.
  • This count can be determined by using binary search . The C++ inbuilt function upper_bound can be used directly.

Below is the implementation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return total
// number of intersections
int FindIntersection(pair<int, int> type1[], int n,
                     pair<int, int> type2[], int m)
{
  
    // Maximum possible number
    // of intersections
    int ans = n * m;
  
    vector<int> start, end;
    for (int i = 0; i < n; i++) {
  
        // Store all starting
        // points of type1 ranges
        start.push_back(type1[i].first);
  
        // Store all endpoints
        // of type1 ranges
        end.push_back(type1[i].second);
    }
  
    sort(start.begin(), start.end());
    sort(end.begin(), end.end());
  
    for (int i = 0; i < m; i++) {
  
        // Starting point of type2 ranges
        int L = type2[i].first;
  
        // Ending point of type2 ranges
        int R = type2[i].second;
  
        // Subtract those ranges which
        // are starting after R
        ans -= (start.end() - 
        upper_bound(start.begin(), start.end(), R));
  
        // Subtract those ranges which
        // are ending before L
        ans -= 
        (upper_bound(end.begin(), end.end(), L - 1) 
        - end.begin());
    }
  
    return ans;
}
  
// Driver Code
int main()
{
  
    pair<int, int> type1[] =
    { { 1, 2 }, { 2, 3 }, { 4, 5 }, { 6, 7 } };
  
    pair<int, int> type2[] = 
    { { 1, 5 }, { 2, 3 }, { 4, 7 }, { 5, 7 } };
  
    int n = sizeof(type1) / (sizeof(type1[0]));
    int m = sizeof(type2) / sizeof(type2[0]);
  
    cout << FindIntersection(type1, n, type2, m);
  
    return 0;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of above approach
from bisect import bisect as upper_bound
  
# Function to return total
# number of intersections
def FindIntersection(type1, n, type2, m):
  
    # Maximum possible number
    # of intersections
    ans = n * m
  
    start = []
    end = []
    for i in range(n):
          
        # Store all starting
        # points of type1 ranges
        start.append(type1[i][0])
  
        # Store all endpoints
        # of type1 ranges
        end.append(type1[i][1])
  
    start = sorted(start)
    start = sorted(end)
  
    for i in range(m):
  
        # Starting poof type2 ranges
        L = type2[i][0]
  
        # Ending poof type2 ranges
        R = type2[i][1]
  
        # Subtract those ranges which
        # are starting after R
        ans -= (len(start)- upper_bound(start, R))
  
        # Subtract those ranges which
        # are ending before L
        ans -= (upper_bound(end, L - 1))
  
    return ans
  
# Driver Code
type1 = [ [ 1, 2 ], [ 2, 3 ], 
          [ 4, 5 ], [ 6, 7 ] ]
  
type2 = [ [ 1, 5 ], [ 2, 3 ], 
          [ 4, 7 ], [ 5, 7 ] ]
  
n = len(type1)
m = len(type2)
  
print(FindIntersection(type1, n, type2, m))
  
# This code is contributed by Mohit Kumar

chevron_right


Output:

9

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : mohit kumar 29