Find maximum points which can be obtained by deleting elements from array | function to return maximum cost obtained ; find maximum element of the array . ; initialize count of all elements to zero . ; calculate frequency of all elements of array . ; stores cost of deleted elements . ; selecting minimum range from L and R . ; finds upto which elements are to be deleted when element num is selected . ; get maximum when selecting element num or not . ; Driver Code ; size of array ; function call to find maximum cost
function maxCost ( a , n , l , r ) { var mx = 0 , k ; for ( var i = 0 ; i < n ; ++ i ) mx = Math . max ( mx , a [ i ] ) ; var count = new Array ( mx + 1 ) ; count . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) count [ a [ i ] ] ++ ; var res = new Array ( mx + 1 ) ; res [ 0 ] = 0 ; l = Math . min ( l , r ) ; for ( var num = 1 ; num <= mx ; num ++ ) { k = Math . max ( num - l - 1 , 0 ) ; res [ num ] = Math . max ( res [ num - 1 ] , num * count [ num ] + res [ k ] ) ; } return res [ mx ] ; } var a = [ 2 , 1 , 2 , 3 , 2 , 2 , 1 ] ; var l = 1 , r = 1 ; var n = a . length ; document . write ( maxCost ( a , n , l , r ) ) ;