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
function maxRanges ( arr , N ) { arr . sort ( ( a , b ) => { return b [ 0 ] - a [ 0 ] } ) let count = 1 ; let prev = arr [ 0 ] ; for ( let i = 1 ; i < N ; i ++ ) { let last = arr [ i - 1 ] ; let current = arr [ i ] ; if ( last [ 0 ] == current [ 0 ] && last [ 1 ] == current [ 1 ] && current [ 1 ] == current [ 0 ] ) { continue ; } let start = Math . max ( prev [ 0 ] , current [ 0 ] - 1 ) ; let end = Math . max ( prev [ 1 ] , current [ 1 ] ) ; if ( ( end - start ) > 0 ) { prev [ 0 ] = 1 + start ; prev [ 1 ] = end ; count ++ ; } } return count ; } let range = [ [ 1 , 4 ] , [ 4 , 4 ] , [ 2 , 2 ] , [ 3 , 4 ] , [ 1 , 1 ] ] ; let N = range . length ; document . write ( maxRanges ( range , N ) ) ;