Maximum points covered after removing an Interval | Function To find the required interval ; Total Count of covered numbers ; Array to store numbers that occur exactly in one interval till ith interval ; Calculate New count by removing all numbers in range [ l , r ] occurring exactly once ; Driver code
function solve ( interval , N , Q ) { var Mark = Array ( Q ) . fill ( 0 ) ; for ( var i = 0 ; i < N ; i ++ ) { var l = interval [ i ] [ 0 ] - 1 ; var r = interval [ i ] [ 1 ] - 1 ; for ( var j = l ; j <= r ; j ++ ) Mark [ j ] ++ ; } var count = 0 ; for ( var i = 0 ; i < Q ; i ++ ) { if ( Mark [ i ] ) count ++ ; } var count1 = Array ( Q ) . fill ( 0 ) ; if ( Mark [ 0 ] == 1 ) count1 [ 0 ] = 1 ; for ( var i = 1 ; i < Q ; i ++ ) { if ( Mark [ i ] == 1 ) count1 [ i ] = count1 [ i - 1 ] + 1 ; else count1 [ i ] = count1 [ i - 1 ] ; } var maxindex ; var maxcoverage = 0 ; for ( var i = 0 ; i < N ; i ++ ) { var l = interval [ i ] [ 0 ] - 1 ; var r = interval [ i ] [ 1 ] - 1 ; var elem1 ; if ( l != 0 ) elem1 = count1 [ r ] - count1 [ l - 1 ] ; else elem1 = count1 [ r ] ; if ( count - elem1 >= maxcoverage ) { maxcoverage = count - elem1 ; maxindex = i ; } } document . write ( " " + maxcoverage + " " + maxindex ) ; } var interval = [ [ 1 , 4 ] , [ 4 , 5 ] , [ 5 , 6 ] , [ 6 , 7 ] , [ 3 , 5 ] ] ; var N = interval . length ; var Q = 7 ; solve ( interval , N , Q ) ;