Find a pair of overlapping intervals from a given Set
Last Updated :
15 Jun, 2021
Given a 2D array arr[][] with each row of the form {l, r}, the task is to find a pair (i, j) such that the ith interval lies within the jth interval. If multiple solutions exist, then print anyone of them. Otherwise, print -1.
Examples:
Input: N = 5, arr[][] = { { 1, 5 }, { 2, 10 }, { 3, 10}, {2, 2}, {2, 15}}
Output: 3 0
Explanation: [2, 2] lies inside [1, 5].
Input: N = 4, arr[][] = { { 2, 10 }, { 1, 9 }, { 1, 8 }, { 1, 7 } }
Output: -1
Explanation: No such pair of intervals exist.
Native Approach: The simplest approach to solve this problem is to generate all possible pairs of the array. For every pair (i, j), check if the ith interval lies within the jth interval or not. If found to be true, then print the pairs. Otherwise, print -1.
Time Complexity: O(N2)
Auxiliary Space:O(1)
Efficient Approach: The idea is to sort the segments firstly by their left border in increasing order and in case of equal left borders, sort them by their right borders in decreasing order. Then, just find the intersecting intervals by keeping track of the maximum right border.
Follow the steps below to solve the problem:
- Sort the given array of intervals according to their left border and if any two left borders are equal, sort them with their right border in decreasing order.
- Now, traverse from left to right, keep the maximum right border of processed segments and compare it to the current segment.
- If the segments are overlapping, print their indices.
- Otherwise, after traversing, if no overlapping segments are found, print -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findOverlapSegement( int N, int a[], int b[])
{
vector<pair<pair< int , int >, int > > tup;
for ( int i = 0; i < N; i++) {
int x, y;
x = a[i];
y = b[i];
tup.push_back(pair<pair< int , int >, int >(
pair< int , int >(x, y), i));
}
sort(tup.begin(), tup.end());
int curr = tup[0].first.second;
int currPos = tup[0].second;
for ( int i = 1; i < N; i++) {
int Q = tup[i - 1].first.first;
int R = tup[i].first.first;
if (Q == R) {
if (tup[i - 1].first.second
< tup[i].first.second)
cout << tup[i - 1].second << ' '
<< tup[i].second;
else
cout << tup[i].second << ' '
<< tup[i - 1].second;
return ;
}
int T = tup[i].first.second;
if (T <= curr) {
cout << tup[i].second << ' ' << currPos;
return ;
}
else {
curr = T;
currPos = tup[i].second;
}
}
cout << "-1 -1" ;
}
int main()
{
int a[] = { 1, 2, 3, 2, 2 };
int b[] = { 5, 10, 10, 2, 15 };
int N = sizeof (a) / sizeof ( int );
findOverlapSegement(N, a, b);
}
|
Java
import java.util.*;
import java.lang.*;
class pair{
int l,r,index;
pair( int l, int r, int index){
this .l = l;
this .r = r;
this .index=index;
}
}
class GFG {
static void findOverlapSegement( int N, int [] a, int [] b)
{
ArrayList<pair> tup = new ArrayList<>();
for ( int i = 0 ; i < N; i++) {
int x, y;
x = a[i];
y = b[i];
tup.add( new pair(x, y, i));
}
Collections.sort(tup,(aa,bb)->(aa.l!=bb.l)?aa.l-bb.l:aa.r-bb.r);
int curr = tup.get( 0 ).r;
int currPos = tup.get( 0 ).index;
for ( int i = 1 ; i < N; i++) {
int Q = tup.get(i - 1 ).l;
int R = tup.get(i).l;
if (Q == R) {
if (tup.get(i - 1 ).r < tup.get(i).r)
System.out.print(tup.get(i - 1 ).index + " " + tup.get(i).index);
else
System.out.print(tup.get(i).index + " " + tup.get(i - 1 ).index);
return ;
}
int T = tup.get(i).r;
if (T <= curr) {
System.out.print(tup.get(i).index + " " + currPos);
return ;
}
else {
curr = T;
currPos = tup.get(i).index;
}
}
System.out.print( "-1 -1" );
}
public static void main (String[] args)
{
int [] a = { 1 , 2 , 3 , 2 , 2 };
int [] b = { 5 , 10 , 10 , 2 , 15 };
int N = a.length;
findOverlapSegement(N, a, b);
}
}
|
Python3
def findOverlapSegement(N, a, b) :
tup = []
for i in range (N) :
x = a[i]
y = b[i]
tup.append(((x,y),i))
tup.sort()
curr = tup[ 0 ][ 0 ][ 1 ]
currPos = tup[ 0 ][ 1 ]
for i in range ( 1 ,N) :
Q = tup[i - 1 ][ 0 ][ 0 ]
R = tup[i][ 0 ][ 0 ]
if Q = = R :
if tup[i - 1 ][ 0 ][ 1 ] < tup[i][ 0 ][ 1 ] :
print (tup[i - 1 ][ 1 ], tup[i][ 1 ])
else :
print (tup[i][ 1 ], tup[i - 1 ][ 1 ])
return
T = tup[i][ 0 ][ 1 ]
if (T < = curr) :
print (tup[i][ 1 ], currPos)
return
else :
curr = T
currPos = tup[i][ 1 ]
print ( "-1" , "-1" , end = "")
a = [ 1 , 2 , 3 , 2 , 2 ]
b = [ 5 , 10 , 10 , 2 , 15 ]
N = len (a)
findOverlapSegement(N, a, b)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void findOverlapSegement( int N, int [] a, int [] b)
{
List<Tuple<Tuple< int , int >, int >> tup = new List<Tuple<Tuple< int , int >, int >>();
for ( int i = 0; i < N; i++) {
int x, y;
x = a[i];
y = b[i];
tup.Add( new Tuple<Tuple< int , int >, int >( new Tuple< int , int >(x, y), i));
}
tup.Sort();
int curr = tup[0].Item1.Item2;
int currPos = tup[0].Item2;
for ( int i = 1; i < N; i++) {
int Q = tup[i - 1].Item1.Item1;
int R = tup[i].Item1.Item1;
if (Q == R) {
if (tup[i - 1].Item1.Item2 < tup[i].Item1.Item2)
Console.Write(tup[i - 1].Item2 + " " + tup[i].Item2);
else
Console.Write(tup[i].Item2 + " " + tup[i - 1].Item2);
return ;
}
int T = tup[i].Item1.Item2;
if (T <= curr) {
Console.Write(tup[i].Item2 + " " + currPos);
return ;
}
else {
curr = T;
currPos = tup[i].Item2;
}
}
Console.Write( "-1 -1" );
}
static void Main()
{
int [] a = { 1, 2, 3, 2, 2 };
int [] b = { 5, 10, 10, 2, 15 };
int N = a.Length;
findOverlapSegement(N, a, b);
}
}
|
Javascript
<script>
function findOverlapSegement(N, a, b)
{
var tup = [];
for ( var i = 0; i < N; i++) {
var x, y;
x = a[i];
y = b[i];
tup.push([[x, y], i]);
}
tup.sort((a,b) =>
{
if (a[0][0] == b[0][0])
{
return a[0][1] - b[0][1];
}
var tmp = (a[0][0] - b[0][0]);
console.log(tmp);
return (a[0][0] - b[0][0])
});
var curr = tup[0][0][1];
var currPos = tup[0][1];
for ( var i = 1; i < N; i++) {
var Q = tup[i - 1][0][0];
var R = tup[i][0][0];
if (Q == R) {
if (tup[i - 1][0][1]
< tup[i][0][1]) {
document.write(tup[i - 1][1] + " " + tup[i][1]);
return ;
}
else {
document.write(tup[i][1] + " " + tup[i - 1][1]);
return ;
}
}
var T = tup[i][0][1];
if (T <= curr) {
document.write(tup[i][1] + " " + currPos);
return ;
}
else {
curr = T;
currPos = tup[i][1];
}
}
document.write( "-1 -1" );
}
let a = [ 1, 2, 3, 2, 2 ];
let b = [ 5, 10, 10, 2, 15 ];
let N = a.length;
findOverlapSegement(N, a, b);
</script>
|
Time Complexity: O(N * log(N))
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...