Maximum of sum of length of rectangles and squares formed by given sticks | Function to find the maximum of sum of lengths of rectangles and squares formed using given set of sticks ; Stores the count of frequencies of all the array elements ; Stores frequencies which are at least 2 ; Convert all frequencies to nearest smaller even values . ; Sum of elements up to index of multiples of 4 ; Print the sum ; Driver Code
function findSumLength ( arr , n ) { let freq = new Map ( ) ; for ( let i = 0 ; i < arr . length ; i ++ ) { if ( freq . has ( arr [ i ] ) ) freq . set ( arr [ i ] , freq . get ( arr [ i ] ) + 1 ) ; else freq . set ( arr [ i ] , 1 ) ; } let freq_2 = new Map ( ) ; for ( let [ key , value ] of freq . entries ( ) ) { if ( freq . get ( key ) >= 2 ) freq_2 . set ( key , freq . get ( key ) ) ; } let arr1 = [ ] ; for ( let [ key , value ] of freq_2 . entries ( ) ) arr1 . push ( key * Math . floor ( freq_2 . get ( key ) / 2 ) * 2 ) ; arr1 . sort ( function ( a , b ) { return a - b ; } ) ; let summ = 0 ; for ( let i = 0 ; i < arr1 . length ; i ++ ) { summ += arr1 [ i ] ; } document . write ( summ ) ; } let arr = [ 5 , 3 , 2 , 3 , 6 , 4 , 4 , 4 , 5 , 5 , 5 ] ; let n = arr . Count ; findSumLength ( arr , n ) ;