Maximum score of deleting an element from an Array based on given condition | C # implementation to find the maximum score of the deleting a element from an array ; 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
using System ; using System . Linq ; using System . Collections . Generic ; class GFG { static int findMaximumScore ( int [ ] a , int n ) { Dictionary < int , int > freq = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq . ContainsKey ( a [ i ] ) ) { freq [ a [ i ] ] = freq [ a [ i ] ] + 1 ; } else { freq . Add ( a [ i ] , 1 ) ; } } int [ ] dp = new int [ a . Max ( ) + 1 ] ; dp [ 0 ] = 0 ; dp [ 1 ] = freq [ 1 ] ; for ( int i = 2 ; i < dp . Length ; i ++ ) dp [ i ] = Math . Max ( dp [ i - 1 ] , dp [ i - 2 ] + freq [ i ] * i ) ; return dp [ dp . Length - 1 ] ; } public static void Main ( String [ ] args ) { int n ; n = 3 ; int [ ] a = { 1 , 2 , 3 } ; Console . Write ( findMaximumScore ( a , n ) ) ; } }