Find intersection of intervals given by two lists
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 if 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++
#include <bits/stdc++.h>
using namespace std;
void printIntervals(vector<vector< int > > arr1,
vector<vector< int > > arr2)
{
int i = 0, j = 0;
int n = arr1.size(), m = arr2.size();
while (i < n && j < m) {
int l = max(arr1[i][0], arr2[j][0]);
int r = min(arr1[i][1], arr2[j][1]);
if (l <= r)
cout << "{" << l << ", "
<< r << "}\n" ;
if (arr1[i][1] < arr2[j][1])
i++;
else
j++;
}
}
int main()
{
vector<vector< int > > arr1
= { { 0, 4 }, { 5, 10 },
{ 13, 20 }, { 24, 25 } };
vector<vector< int > > arr2
= { { 1, 5 }, { 8, 12 },
{ 15, 24 }, { 25, 26 } };
printIntervals(arr1, arr2);
return 0;
}
|
Java
class GFG{
static void printIntervals( int arr1[][],
int arr2[][])
{
int i = 0 , j = 0 ;
int n = arr1.length, m = arr2.length;
while (i < n && j < m)
{
int l = Math.max(arr1[i][ 0 ], arr2[j][ 0 ]);
int r = Math.min(arr1[i][ 1 ], arr2[j][ 1 ]);
if (l <= r)
System.out.println( "{" + l + ", " +
r + "}" );
if (arr1[i][ 1 ] < arr2[j][ 1 ])
i++;
else
j++;
}
}
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);
}
}
|
Python3
def printIntervals(arr1, arr2):
i = j = 0
n = len (arr1)
m = len (arr2)
while i < n and j < m:
l = max (arr1[i][ 0 ], arr2[j][ 0 ])
r = min (arr1[i][ 1 ], arr2[j][ 1 ])
if l < = r:
print ( '{' , l, ',' , r, '}' )
if arr1[i][ 1 ] < arr2[j][ 1 ]:
i + = 1
else :
j + = 1
arr1 = [ [ 0 , 4 ], [ 5 , 10 ],
[ 13 , 20 ], [ 24 , 25 ] ]
arr2 = [ [ 1 , 5 ], [ 8 , 12 ],
[ 15 , 24 ], [ 25 , 26 ] ]
printIntervals(arr1, arr2)
|
C#
using System;
class GFG{
static void printIntervals( int [,]arr1,
int [,]arr2)
{
int i = 0, j = 0;
int n = arr1.GetLength(0),
m = arr2.GetLength(0);
while (i < n && j < m)
{
int l = Math.Max(arr1[i, 0], arr2[j, 0]);
int r = Math.Min(arr1[i, 1], arr2[j, 1]);
if (l <= r)
Console.WriteLine( "{" + l + ", " +
r + "}" );
if (arr1[i, 1] < arr2[j, 1])
i++;
else
j++;
}
}
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);
}
}
|
Javascript
<script>
function printIntervals(arr1, arr2)
{
var i = 0, j = 0;
var n = arr1.length, m = arr2.length;
while (i < n && j < m)
{
var l = Math.max(arr1[i][0], arr2[j][0]);
var r = Math.min(arr1[i][1], arr2[j][1]);
if (l <= r)
document.write( "{" + l + ", " +
r + "}" + "<br>" );
if (arr1[i][1] < arr2[j][1])
i++;
else
j++;
}
}
var arr1 = [ [ 0, 4 ], [ 5, 10 ],
[ 13, 20 ], [ 24, 25 ] ];
var arr2 = [ [ 1, 5 ], [ 8, 12 ],
[ 15, 24 ], [ 25, 26 ] ];
printIntervals(arr1, arr2);
</script>
|
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
Auxiliary Space: O(1)
Last Updated :
22 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...