Maximum score of deleting an element from an Array based on given condition | Java 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
import java . util . * ; class GFG { static int findMaximumScore ( int [ ] a , int n ) { @ SuppressWarnings ( " unchecked " ) HashMap < Integer , Integer > freq = new HashMap ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq . containsKey ( a [ i ] ) ) { freq . put ( a [ i ] , freq . get ( a [ i ] ) + 1 ) ; } else { freq . put ( a [ i ] , 1 ) ; } } int [ ] dp = new int [ Arrays . stream ( a ) . max ( ) . getAsInt ( ) + 1 ] ; dp [ 0 ] = 0 ; dp [ 1 ] = freq . get ( 1 ) ; for ( int 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 ] ; } public static void main ( String [ ] args ) { int n ; n = 3 ; int [ ] a = { 1 , 2 , 3 } ; System . out . print ( findMaximumScore ( a , n ) ) ; } }