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.
functionminiMaxSum(arr:number[]):void{constsortedArray=arr.sort((a,b)=>a-b);consttotalSum=sortedArray.reduce((a,b)=>a+b,0);constminSum=totalSum-sortedArray[arr.length-1];constmaxSum=totalSum-sortedArray[0];console.log(minSum,maxSum);}// Simple, but with higher time complexity O(n log n)
Solution 2
functionminiMaxSum(arr:number[]):void{letminNum=Number.MAX_SAFE_INTEGER;letmaxNum=1;lettotalSum=0;arr.forEach((num)=>{totalSum+=num;if(num<minNum){minNum=num;}if(num>maxNum){maxNum=num;}});console.log(totalSum-maxNum,totalSum-minNum);}// More complex, but with lower time complexity O(n)
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 →
TypeScript Solutions
Solution 1
Solution 2