Maximum of sum of length of rectangles and squares formed by given sticks | Python3 implementation of the above approach ; 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
from collections import Counter NEW_LINE def findSumLength ( arr , n ) : NEW_LINE INDENT freq = dict ( Counter ( arr ) ) NEW_LINE freq_2 = { } NEW_LINE for i in freq : NEW_LINE INDENT if freq [ i ] >= 2 : NEW_LINE INDENT freq_2 [ i ] = freq [ i ] NEW_LINE DEDENT DEDENT arr1 = [ ] NEW_LINE for i in freq_2 : NEW_LINE arr1 . extend ( [ i ] * ( freq_2 [ i ] // 2 ) * 2 ) NEW_LINE arr1 . sort ( reverse = True ) NEW_LINE summ = 0 NEW_LINE for i in range ( ( len ( arr1 ) // 4 ) * 4 ) : NEW_LINE INDENT summ += arr1 [ i ] NEW_LINE DEDENT print ( summ ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 3 , 2 , 3 , 6 , 4 , 4 , 4 , 5 , 5 , 5 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE findSumLength ( arr , n ) ; NEW_LINE DEDENT