Find a pair of intersecting ranges from a given array | Store ranges and their corresponding array indices ; Function to find a pair of intersecting ranges ; Stores ending point of every range ; Stores the maximum ending point obtained ; Iterate from 0 to N - 1 ; Starting point of the current range ; End point of the current range ; Push pairs into tup ; Sort the tup vector sort ( tup . begin ( ) , tup . end ( ) ) ; ; Iterate over the ranges ; If starting points are equal ; Print the indices of the intersecting ranges ; If no such pair of segments exist ; Given N ; Given 2d ranges [ ] [ ] array ; Function call
let tup = [ ] ; function findIntersectingRange ( N , ranges ) { let curr ; let currPos ; for ( let i = 0 ; i < N ; i ++ ) { let x , y ; x = ranges [ i ] [ 0 ] ; y = ranges [ i ] [ 1 ] ; let arr = [ x , y , i + 1 ] ; tup . push ( arr ) ; } tup . sort ( function ( a , b ) { if ( a [ 0 ] == b [ 0 ] ) { if ( a [ 1 ] == b [ 1 ] ) { return a [ 2 ] - b [ 2 ] ; } else { return a [ 1 ] - b [ 1 ] ; } } return a [ 0 ] - b [ 0 ] ; } ) ; curr = tup [ 0 ] [ 1 ] ; currPos = tup [ 0 ] [ 2 ] ; for ( let i = 1 ; i < N ; i ++ ) { let Q = tup [ i - 1 ] [ 0 ] ; let R = tup [ i ] [ 0 ] ; if ( Q == R ) { if ( tup [ i - 1 ] [ 1 ] < tup [ i ] [ 1 ] ) document . write ( tup [ i - 1 ] [ 2 ] + " " + tup [ i ] [ 2 ] ) ; else document . write ( tup [ i ] [ 2 ] + " " + tup [ i - 1 ] [ 2 ] ) ; return ; } let T = tup [ i ] [ 1 ] ; if ( T <= curr ) { document . write ( tup [ i ] [ 2 ] + " " + currPos ) ; return ; } else { curr = T ; currPos = tup [ i ] [ 2 ] ; } } document . write ( " " ) ; } let N = 5 ; let ranges = [ [ 1 , 5 ] , [ 2 , 10 ] , [ 3 , 10 ] , [ 2 , 2 ] , [ 2 , 15 ] ] ; findIntersectingRange ( N , ranges ) ;