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.
- Prepare
- Algorithms
- Warmup
- Mini-Max Sum
- Discussions
Mini-Max Sum
Mini-Max Sum
Sort by
recency
|
6078 Discussions
|
Please Login in order to post a comment
void miniMaxSum(vector arr) { if(arr.size() == 0) return; sort(arr.begin(), arr.end()); long long int min = accumulate(arr.begin()+0, arr.begin()+4, 0); long long int max = accumulate(arr.begin()+1, arr.begin()+5, 0); cout << min << " " << max; }
This is the approach I came with but some of the test cases are failing
def miniMaxSum(arr): s=0 m=0 arr.sort() for i in range(1,len(arr)): s=s+arr[i] for i in range(0,len(arr)-1): m=m+arr[i]
print(m,s)
For Python3 Platform
I wrote the code from scratch just to get more practice
The trick with this “mini-max sum” is super simple but kinda poetic: sometimes the biggest answer hides when you drop the smallest piece, and the smallest answer shows up when you let go of the biggest. Math and life got same rules – what you choose to leave out changes the whole picture.
let totalSum = arr.reduce((acc, num) => acc + num, 0);
let minVal = Math.min(...arr);
let maxVal = Math.max(...arr);
let minSum = totalSum - maxVal; let maxSum = totalSum - minVal;