Mini-Max Sum

Max N
The Startup
Published in
4 min readNov 16, 2019

--

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:

--

--

Max N
The Startup

A writer that writes about JavaScript and Python to beginners. If you find my articles helpful, feel free to follow.