Maximum score of deleting an element from an Array based on given condition | Function to find the maximum score of the deleting an element from an array ; Creating a map to keep the frequency of numbers ; Loop to iterate over the elements of the array ; Creating a DP array to keep count of max score at ith element and it will be filled in the bottom Up manner ; Loop to choose the elements of the array to delete from the array ; Driver Code ; Function call
function findMaximumScore ( a , n ) { let freq = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( freq . has ( a [ i ] ) ) { freq . set ( a [ i ] , freq . get ( a [ i ] ) + 1 ) ; } else { freq . set ( a [ i ] , 1 ) ; } } let dp = new Array ( Math . max ( ... a ) + 1 ) ; dp [ 0 ] = 0 ; dp [ 1 ] = freq . get ( 1 ) ; for ( let i = 2 ; i < dp . length ; i ++ ) dp [ i ] = Math . max ( dp [ i - 1 ] , dp [ i - 2 ] + freq . get ( i ) * i ) ; return dp [ dp . length - 1 ] ; } let n = 3 ; let a = [ 1 , 2 , 3 ] ; document . write ( findMaximumScore ( a , n ) ) ;