Minimum cost to make array size 1 by removing larger of pairs | function to calculate the minimum cost ; find the minimum using for loop ; Minimum cost is n - 1 multiplied with minimum element . ; Driver code
function cost ( a , n ) { let min = a [ 0 ] ; for ( let i = 1 ; i < a . length ; i ++ ) { if ( a [ i ] < min ) min = a [ i ] ; } return ( n - 1 ) * min ; } let a = [ 4 , 3 , 2 ] ; let n = a . length ; document . write ( cost ( a , n ) ) ;