Maximum number of overlapping Intervals | Function that print maximum overlap among ranges ; Variable to store the maximum count ; Storing the x and y coordinates in data vector ; Pushing the x coordinate ; Pushing the y coordinate ; Sorting of ranges ; Traverse the data vector to count number of overlaps ; If x occur it means a new range is added so we increase count ; If y occur it means a range is ended so we decrease count ; Updating the value of ans after every traversal ; Printing the maximum value ; Driver code
function overlap ( v ) { var ans = 0 ; var count = 0 ; var data = [ ] ; for ( var i = 0 ; i < v . length ; i ++ ) { data . push ( [ v [ i ] [ 0 ] , ' ' ] ) ; data . push ( [ v [ i ] [ 1 ] , ' ' ] ) ; } data . sort ( ) ; for ( var i = 0 ; i < data . length ; i ++ ) { if ( data [ i ] [ 1 ] == ' ' ) count ++ ; if ( data [ i ] [ 1 ] == ' ' ) count -- ; ans = Math . max ( ans , count ) ; } document . write ( ans + " " ) ; } var v = [ [ 1 , 2 ] , [ 2 , 4 ] , [ 3 , 6 ] ] ; overlap ( v ) ;