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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findSumLength ( vector < int > arr , int n ) { map < int , int > freq ; for ( int i : arr ) freq [ i ] += 1 ; map < int , int > freq_2 ; for ( auto i : freq ) { if ( freq [ i . first ] >= 2 ) freq_2 [ i . first ] = freq [ i . first ] ; } vector < int > arr1 ; for ( auto i : freq_2 ) arr1 . push_back ( ( i . first ) * ( freq_2 [ ( i . first ) ] / 2 ) * 2 ) ; sort ( arr1 . begin ( ) , arr1 . end ( ) ) ; int summ = 0 ; for ( int i : arr1 ) summ += i ; cout << summ ; } int main ( ) { vector < int > arr = { 5 , 3 , 2 , 3 , 6 , 4 , 4 , 4 , 5 , 5 , 5 } ; int n = arr . size ( ) ; findSumLength ( arr , n ) ; return 0 ; }