EkaterinaRomankova / Codewars

Codewars
0 stars 0 forks source link

Minimize Sum Of Array (Array Series #1) #28

Open EkaterinaRomankova opened 2 years ago

EkaterinaRomankova commented 2 years ago

Given an array of integers , Find the minimum sum which is obtained from summing each Two integers product .

Notes

Array/list will contain positives only . Array/list will always have even size Input >> Output Examples

minSum({5,4,2,3}) ==> return (22) Explanation:

The minimum sum obtained from summing each two integers product , 52 + 34 = 22 minSum({12,6,10,26,3,24}) ==> return (342) Explanation:

The minimum sum obtained from summing each two integers product , 263 + 246 + 12*10 = 342 minSum({9,2,8,7,5,4,0,6}) ==> return (74) Explanation:

The minimum sum obtained from summing each two integers product , 90 + 82 +74 +65 = 74

function minSum(arr) { let arr1 = arr.sort((a, b) => a - b); let sum = 0; for(let i = 0; i < arr1.length / 2; i++){ sum += arr1[i] * arr1[arr1.length - 1 - i] } return sum }

function minSum(arr) { var res=0; arr = arr.sort((a,b)=>a-b);

while(arr.length){ res += arr.pop()*arr.shift(); }

return res }