Maximum ranges that can be uniquely represented by any integer from the range | Function to find the maximum number of ranges where each range can be uniquely represented by an integer ; Sort the ranges in ascending order ; Stores the count of ranges ; Stores previously assigned range ; Traverse the vector arr [ ] ; Skip the similar ranges of size 1 ; Find the range of integer available to be assigned ; Check if an integer is available to be assigned ; Update the previously assigned range ; Update the count of range ; Return the maximum count of ranges ; Driver Code
def maxRanges ( arr , N ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE count = 1 NEW_LINE prev = arr [ 0 ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT last = arr [ i - 1 ] NEW_LINE current = arr [ i ] NEW_LINE if ( last [ 0 ] == current [ 0 ] and last [ 1 ] == current [ 1 ] and current [ 1 ] == current [ 0 ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT start = max ( prev [ 0 ] , current [ 0 ] - 1 ) NEW_LINE end = max ( prev [ 1 ] , current [ 1 ] ) NEW_LINE if ( end - start > 0 ) : NEW_LINE INDENT prev [ 0 ] = 1 + start NEW_LINE prev [ 1 ] = end NEW_LINE count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ [ 1 , 4 ] , [ 4 , 4 ] , [ 2 , 2 ] , [ 3 , 4 ] , [ 1 , 1 ] ] NEW_LINE N = len ( arr ) NEW_LINE print ( maxRanges ( arr , N ) ) NEW_LINE DEDENT