Remove all occurrences of any element for maximum array sum | Java program to convert fractional decimal to binary number ; Find total sum and frequencies of elements ; Find minimum value to be subtracted . ; Find maximum sum after removal ; Drivers code
import java . util . * ; class GFG { static int maxSumArray ( int arr [ ] , int n ) { int sum = 0 ; Map < Integer , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( m . containsKey ( arr [ i ] ) ) { m . put ( arr [ i ] , m . get ( arr [ i ] ) + 1 ) ; } else { m . put ( arr [ i ] , 1 ) ; } } int minimum = Integer . MAX_VALUE ; for ( Map . Entry < Integer , Integer > x : m . entrySet ( ) ) minimum = Math . min ( minimum , x . getValue ( ) * x . getKey ( ) ) ; return ( sum - minimum ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 3 , 3 , 2 , 2 , 1 , 1 , 1 } ; int n = arr . length ; System . out . println ( maxSumArray ( arr , n ) ) ; } }