We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Simple Javascript solution here, works by sorting the initial array and then reducing the first 4 indices, then last 4 indices to return the min and max sum
function miniMaxSum(arr) {
// Write your code here
let sorted = [...arr.sort((a, b)=>{return a-b})];
console.log(sorted.slice(0, 4).reduce((a, b)=>{
return a+b
}, 0), sorted.slice(1).reduce((a, b)=>{
return a+b
}, 0))
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Mini-Max Sum
You are viewing a single comment's thread. Return to all comments →
Simple Javascript solution here, works by sorting the initial array and then reducing the first 4 indices, then last 4 indices to return the min and max sum
function miniMaxSum(arr) { // Write your code here let sorted = [...arr.sort((a, b)=>{return a-b})]; console.log(sorted.slice(0, 4).reduce((a, b)=>{ return a+b }, 0), sorted.slice(1).reduce((a, b)=>{ return a+b }, 0)) }