Related Articles
Find intersection of intervals given by two lists
• Difficulty Level : Medium
• Last Updated : 10 Aug, 2020

Given two 2-D arrays which represent intervals. Each 2-D array represents a list of intervals. Each list of intervals is disjoint and sorted in increasing order. Find the intersection or set of ranges that are common to both the lists.

Disjoint means no element is common in a list. Example: {1, 4} and {5, 6} are disjoint while {1, 4} and {2, 5} are not as 2, 3 and 4 are common to both intervals.

Examples:

Input: arr1[][] = {{0, 4}, {5, 10}, {13, 20}, {24, 25}}
arr2[][] = {{1, 5}, {8, 12}, {15, 24}, {25, 26}}
Output: {{1, 4}, {5, 5}, {8, 10}, {15, 20}, {24, 24}, {25, 25}}
Explanation:
{1, 4} lies completely within range {0, 4} and {1, 5}. Hence, {1, 4} is the desired intersection. Similarly, {24, 24} lies completely within two intervals {24, 25} and {15, 24}.

Input: arr1[][] = {{0, 2}, {5, 10}, {12, 22}, {24, 25}}
arr2[][] = {{1, 4}, {9, 12}, {15, 24}, {25, 26}}
Output: {{1, 2}, {9, 10}, {12, 12}, {15, 22}, {24, 24}, {25, 25}}
Explanation:
{1, 2} lies completely within range {0, 2} and {1, 4}. Hence, {1, 2} is the desired intersection. Similarly, {12, 12} lies completely within two intervals {12, 22} and {9, 12}.

Approach:
To solve the problem mentioned above, two pointer technique can be used, as per the steps given below:

• Maintain two pointers i and j to traverse the two interval lists, arr1 and arr2 respectively.
• Now, if arr1[i] has smallest endpoint, it can only intersect with arr2[j]. Similarly, if arr2[j] has smallest endpoint, it can only intersect with arr1[i]. If intersection occurs, find the intersecting segment.
• [l, r] will be the intersecting segment iff l <= r, where l = max(arr1[i][0], arr2[j][0]) and r = min(arr1[i][1], arr2[j][1]).
• Increment the i and j pointers accordingly to move ahead.

Below is the implementation of the approach:

## C++

 `// C++ implementation to find the``// intersection of the two intervals`` ` `#include ``using` `namespace` `std;`` ` `// Function to print intersecting intervals``void` `printIntervals(vector > arr1,``                    ``vector > arr2)``{`` ` `    ``// i and j pointers for``    ``// arr1 and arr2 respectively``    ``int` `i = 0, j = 0;`` ` `    ``// Size of the two lists``    ``int` `n = arr1.size(), m = arr2.size();`` ` `    ``// Loop through all intervals unless``    ``// one of the interval gets exhausted``    ``while` `(i < n && j < m) {``        ``// Left bound for intersecting segment``        ``int` `l = max(arr1[i][0], arr2[j][0]);`` ` `        ``// Right bound for intersecting segment``        ``int` `r = min(arr1[i][1], arr2[j][1]);`` ` `        ``// If segment is valid print it``        ``if` `(l <= r)``            ``cout << ``"{"` `<< l << ``", "``                 ``<< r << ``"}\n"``;`` ` `        ``// If i-th interval's right``        ``// bound is smaller``        ``// increment i else``        ``// increment j``        ``if` `(arr1[i][1] < arr2[j][1])``            ``i++;``        ``else``            ``j++;``    ``}``}`` ` `// Driver code``int` `main()``{`` ` `    ``vector > arr1``        ``= { { 0, 4 }, { 5, 10 },``            ``{ 13, 20 }, { 24, 25 } };`` ` `    ``vector > arr2``        ``= { { 1, 5 }, { 8, 12 },``            ``{ 15, 24 }, { 25, 26 } };`` ` `    ``printIntervals(arr1, arr2);`` ` `    ``return` `0;``}`

## Java

 `// Java implementation to find ``// intersecting intervals``class` `GFG{`` ` `// Function to print intersecting intervals``static` `void` `printIntervals(``int` `arr1[][],``                           ``int` `arr2[][])``{``     ` `    ``// i and j pointers for arr1 and ``    ``// arr2 respectively``    ``int` `i = ``0``, j = ``0``;``     ` `    ``int` `n = arr1.length, m = arr2.length;``     ` `    ``// Loop through all intervals unless  ``    ``// one of the interval gets exhausted``    ``while` `(i < n && j < m) ``    ``{``         ` `        ``// Left bound for intersecting segment``        ``int` `l = Math.max(arr1[i][``0``], arr2[j][``0``]);`` ` `        ``// Right bound for intersecting segment``        ``int` `r = Math.min(arr1[i][``1``], arr2[j][``1``]);``         ` `        ``// If segment is valid print it``        ``if` `(l <= r) ``            ``System.out.println(``"{"` `+ l + ``", "` `+``                                 ``r + ``"}"``);`` ` `        ``// If i-th interval's right bound is ``        ``// smaller increment i else increment j``        ``if` `(arr1[i][``1``] < arr2[j][``1``])``            ``i++;``        ``else``            ``j++;``    ``}``}`` ` `// Driver code``public` `static` `void` `main(String[] args)``{``    ``int` `arr1[][] = { { ``0``, ``4` `}, { ``5``, ``10` `},``                     ``{ ``13``, ``20` `}, { ``24``, ``25` `} };`` ` `    ``int` `arr2[][] = { { ``1``, ``5` `}, { ``8``, ``12` `}, ``                     ``{ ``15``, ``24` `}, { ``25``, ``26` `} };`` ` `    ``printIntervals(arr1, arr2);``}``}`` ` `// This code is contributed by sarthak_eddy`

## Python3

 `# Python3 implementation to find ``# intersecting intervals`` ` `# Function to print intersecting ``# intervals``def` `printIntervals(arr1, arr2):``     ` `    ``# i and j pointers for arr1 ``    ``# and arr2 respectively``    ``i ``=` `j ``=` `0``     ` `    ``n ``=` `len``(arr1)``    ``m ``=` `len``(arr2)`` ` `    ``# Loop through all intervals unless one ``    ``# of the interval gets exhausted``    ``while` `i < n ``and` `j < m:``         ` `        ``# Left bound for intersecting segment``        ``l ``=` `max``(arr1[i][``0``], arr2[j][``0``])``         ` `        ``# Right bound for intersecting segment``        ``r ``=` `min``(arr1[i][``1``], arr2[j][``1``])``         ` `        ``# If segment is valid print it``        ``if` `l <``=` `r: ``            ``print``(``'{'``, l, ``','``, r, ``'}'``)`` ` `        ``# If i-th interval's right bound is ``        ``# smaller increment i else increment j``        ``if` `arr1[i][``1``] < arr2[j][``1``]:``            ``i ``+``=` `1``        ``else``:``            ``j ``+``=` `1`` ` `# Driver code``arr1 ``=` `[ [ ``0``, ``4` `], [ ``5``, ``10` `],``         ``[ ``13``, ``20` `], [ ``24``, ``25` `] ]`` ` `arr2 ``=` `[ [ ``1``, ``5` `], [ ``8``, ``12` `], ``         ``[ ``15``, ``24` `], [ ``25``, ``26` `] ]`` ` `printIntervals(arr1, arr2)`` ` `# This code is contributed by sarthak_eddy`

## C#

 `// C# implementation to find ``// intersecting intervals``using` `System;``class` `GFG{``     ` `// Function to print intersecting intervals``static` `void` `printIntervals(``int` `[,]arr1,``                           ``int` `[,]arr2)``{``     ` `    ``// i and j pointers for arr1 and ``    ``// arr2 respectively``    ``int` `i = 0, j = 0;``     ` `    ``int` `n = arr1.GetLength(0),``        ``m = arr2.GetLength(0);``     ` `    ``// Loop through all intervals unless ``    ``// one of the interval gets exhausted``    ``while` `(i < n && j < m) ``    ``{``     ` `        ``// Left bound for intersecting segment``        ``int` `l = Math.Max(arr1[i, 0], arr2[j, 0]);``        ` `        ``// Right bound for intersecting segment``        ``int` `r = Math.Min(arr1[i, 1], arr2[j, 1]);``     ` `        ``// If segment is valid print it``        ``if` `(l <= r) ``        ``Console.WriteLine(``"{"` `+ l + ``", "` `+``                            ``r + ``"}"``);``                             ` `        ``// If i-th interval's right bound is ``        ``// smaller increment i else increment j``        ``if` `(arr1[i, 1] < arr2[j, 1])``            ``i++;``        ``else``            ``j++;``    ``}``}`` ` `// Driver code``public` `static` `void` `Main(String[] args)``{``    ``int` `[,]arr1 = { { 0, 4 }, { 5, 10 },``                    ``{ 13, 20 }, { 24, 25 } };``                     ` `    ``int` `[,]arr2 = { { 1, 5 }, { 8, 12 }, ``                    ``{ 15, 24 }, { 25, 26 } };``                     ` `    ``printIntervals(arr1, arr2);``}``}`` ` `// This code is contributed by Princi Singh`
Output:
```{1, 4}
{5, 5}
{8, 10}
{15, 20}
{24, 24}
{25, 25}
```

Time Complexity: O(N + M), where N and M are lengths of the 2-D arrays

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.

My Personal Notes arrow_drop_up
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://www.geeksforgeeks.org/find-intersection-of-intervals-given-by-two-lists/?ref=lbp", "fetch_time": 1618680154000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2021-17/segments/1618038461619.53/warc/CC-MAIN-20210417162353-20210417192353-00120.warc.gz", "warc_record_offset": 872153152, "warc_record_length": 22282, "token_count": 2990, "char_count": 8472, "score": 4.1875, "int_score": 4, "crawl": "CC-MAIN-2021-17", "snapshot_type": "latest", "language": "en", "language_score": 0.8808407783508301, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00003-of-00064.parquet"}