Find a pair of overlapping intervals from a given Set | Function to find a pair ( i , j ) such that i - th interval lies within the j - th interval ; Store interval and index of the interval in the form of { { l , r } , index } ; Traverse the array , arr [ ] [ ] ; Stores l - value of the interval ; Stores r - value of the interval ; Push current interval and index into tup ; Sort the vector based on l - value of the intervals ; Stores r - value of current interval ; Stores index of current interval ; Traverse the vector , tup [ ] ; Stores l - value of previous interval ; Stores l - value of current interval ; If Q and R equal ; If Y value of immediate previous interval is less than Y value of current interval ; Print the index of interval ; Stores r - value of current interval ; T is less than or equal to curr ; Update curr ; Update currPos ; If such intervals found ; Driver Code Given l - value of segments ; Given r - value of segments ; Given size ; Function Call
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 ( " " ) ; } let a = [ 1 , 2 , 3 , 2 , 2 ] ; let b = [ 5 , 10 , 10 , 2 , 15 ] ; let N = a . length ; findOverlapSegement ( N , a , b ) ;