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
|
6077 Discussions
|
Please Login in order to post a comment
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;