Maximum number of parallelograms that can be made using the given length of line segments | Function to find the maximum number of parallelograms can be made ; Finding the length of the frequency array ; Increasing the occurrence of each segment ; To store the count of parallelograms ; Counting parallelograms that can be made using 4 similar sides ; counting segments which have occurrence left >= 2 ; Adding parallelograms that can be made using 2 similar sides ; Driver code
function convert ( n , a ) { let z = a [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { if ( a [ i ] > z ) z = a [ i ] ; } z = z + 1 ; let ff = new Array ( z ) ; ff . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { ff [ a [ i ] ] += 1 ; } let cc = 0 ; for ( let i = 0 ; i < z ; i ++ ) { cc += parseInt ( ff [ i ] / 4 , 10 ) ; ff [ i ] = ff [ i ] % 4 ; } let vv = 0 ; for ( let i = 0 ; i < z ; i ++ ) { if ( ff [ i ] >= 2 ) vv += 1 ; } cc += parseInt ( vv / 2 , 10 ) ; document . write ( cc ) ; } let n = 4 ; let a = [ 1 , 2 , 1 , 2 ] ; convert ( n , a ) ;