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.
- Mini-Max Sum
- Discussions
Mini-Max Sum
Mini-Max Sum
Sort by
recency
|
370 Discussions
|
Please Login in order to post a comment
log(n) in Python:
Shortest solution in Python: def miniMaxSum(arr): # Write your code here
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)) }
Java:
long summ=0,suml=0; Collections.sort(arr); for(int i=0;i<4;i++){ summ+=arr.get(i); } for(int i=arr.size()-1;i>arr.size()-5;i--){ suml+=arr.get(i); } System.out.println(summ+" "+suml); }
c++ short solution using only functions from "algorithm" library