Maximum number of overlapping Intervals | Function that prmaximum 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
def overlap ( v ) : NEW_LINE INDENT ans = 0 NEW_LINE count = 0 NEW_LINE data = [ ] NEW_LINE for i in range ( len ( v ) ) : NEW_LINE INDENT data . append ( [ v [ i ] [ 0 ] , ' x ' ] ) NEW_LINE data . append ( [ v [ i ] [ 1 ] , ' y ' ] ) NEW_LINE DEDENT data = sorted ( data ) NEW_LINE for i in range ( len ( data ) ) : NEW_LINE INDENT if ( data [ i ] [ 1 ] == ' x ' ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT if ( data [ i ] [ 1 ] == ' y ' ) : NEW_LINE INDENT count -= 1 NEW_LINE DEDENT ans = max ( ans , count ) NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT v = [ [ 1 , 2 ] , [ 2 , 4 ] , [ 3 , 6 ] ] NEW_LINE overlap ( v ) NEW_LINE