Mini-Max Sum

Erica N
Erica N
Nov 16, 2019 · 4 min read

For today’s algorithm, we are going to create a function called miniMaxSum. In this function, you are given an array of five integers and the goal is to output the minimum and maximum values that can be calculated when summing four of the five integers in the array.

Here is an example. Let’s create an array called arr:

let arr = [1, 2, 3, 4, 5]

If we were to visually loop through the array, we would sum the remaining four numbers excluding the number the loop is currently at.

The loop gets to the first number, we will exclude the number 1 and sum the remaining four numbers: 2 + 3 + 4 + 5 = 14.

The loop gets to the second number, we will exclude the number 2 and sum the remaining four numbers: 1 + 3 + 4 + 5 = 13.

The loop gets to the third number, we will exclude the number 3 and sum the remaining four numbers: 1 + 2 + 4 + 5 = 12.

The loop gets to the fourth number, we will exclude the number 4 and sum the remaining four numbers: 1 + 2 + 3 + 5 = 11.

The loop gets to the final number, we will exclude the number 5 and sum the remaining four numbers: 1 + 2 + 3 + 4 = 10.

Out of all of the sums, 14 is the max and 10 is our min so our function will output those two numbers on a single line separated by a space:

10 14

Now we are done with our lengthy explanation, let’s turn it into code.

We create our variables:

let totals = [];const reducer = (accumulator, currentValue) => accumulator + currentValue;

The totals variable will collect all of our sums and put each sum in an array.

The reducer variable we will explain later. This will involve using the Array.reduce() method to add the four numbers to get our sum.

First, we loop through our array with a for-loop:

for (let i = 0; i < arr.length; i++) {
let filtered = arr.filter(function(value, index, arr) {
return i !== index;
});
totals.push(filtered.reduce(reducer));
}

On the first line within our for-loop block, we will use the .filter() method to get rid of certain values in an array. What the filter method does is it uses a callback method (a function executing another function after it finishes) and as the filter method iterates through each array item, it will pass three values to the callback: the current value, the array index, and the array itself. When it is done iterating through that particular array value, the callback will return either true or false based on your return statement. The values that return true are added to the filtered array.

When we loop through the array we want to calculate all the items in the array except the current iterated value.

What we do is use the filter method to filter out the value that is equal to the current item being indexed in the for-loop. For example, if the for-loop is currently at the second number, we want the filter method to filter out all numbers that don’t have an index of 1.

The filter method doesn’t touch or mutate the original array. This is important so we always have the original array to filter from for the duration of the for-loop.

The next line:

totals.push(filtered.reduce(reducer));

After the filter method outputs our array of four values, we will add them together using the reduce method.

What the reduce method does is it executes a reducer function on each element of the array. This reduces our array to a single value. Let’s go back to where we listed out our variables and explain what our reducer variable does:

const reducer = (accumulator, currentValue) => accumulator + currentValue;

And to note, the statement above is equivalent to writing:

const reducer = function(accumulator, currentValue){
return accumulator + currentValue;
}

The top uses arrow functions which is another way (and shortened way) to write a function.

The reduce method takes in one required parameter and that is the reducer. Our reducer function takes two parameters, an accumulator and a current value. The reduce method also has an optional parameter called initialValue but we will leave it out. The accumulator will be equal to the first value in the array and the currentValue will be equal to the second.

Going back to our example at the top with [1, 2, 3, 4, 5] we will see the relationship between currentValue and accumulator:

// 2 is the current value. accumulator value updated to 3
1 + 2 = 3
// 3 is the current value. accumulator value updated to 6
3 + 3 = 6
// 4 is the current value. accumulator value updated to 10
6 + 4 = 10
// ... and so on

Every time the current value is added to the accumulator, the accumulator is updated until the array is finished and the final accumulator value is returned.

Back to the for-loop, we push that sum value to the array.

When the loop finishes, we use the Math.max() and Math.min() methods to calculate the maximum and minimum values in our totals array and print them out.

console.log(Math.min(...totals) + " " + Math.max(...totals));

Here is the rest of our code:

function miniMaxSum(arr) {
let totals = [];
const reducer = (accumulator, currentValue) => accumulator + currentValue;
for (let i = 0; i < arr.length; i++) {
let filtered = arr.filter(function(value, index, arr) {
return i !== index;
});
totals.push(filtered.reduce(reducer));
}
console.log(Math.min(...totals) + " " + Math.max(...totals));}

The Startup

Get smarter at building your thing. Join The Startup’s +800K followers.

Erica N

Written by

Erica N

An illustrator that writes humor and satire about everything and nothing at the same time.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +800K followers.

Erica N

Written by

Erica N

An illustrator that writes humor and satire about everything and nothing at the same time.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +800K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store