Question: Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.

For example `var arr = [1,3,5,7,9]` the output will be 16 24.

Here is my solution.everything works except one case.When all element in the arr are equal my solutions returns error. How I can fix it?

```
function miniMaxSum(arr) {
    let largest = arr[0];
    let smallest = arr[0];
    let largestSum = 0;
    let smallestSum = 0;
    for(let i = 0; i < arr.length; i ++){
      if(arr[i] > largest){
        largest = arr[i];
      }
      if (arr[i] < smallest){
        smallest = arr[i];
      }
    }
    for(let j = 0; j < arr.length; j ++){
      if(arr[j] < largest){
        smallestSum = smallestSum + arr[j];
      }
      if(arr[j] > smallest){
        largestSum = largestSum + arr[j];
      }
    }
    console.log(smallestSum + " " + largestSum)
}

```

Answer: 
You could take the first value as start value for `sum`, `min` and `max` value and iterate from the second item. Then add the actual value and check `min` and `max` values and take adjustments.

At the end return the delta of `sum` and `max` and `sum` and `min`.

```js
function minMaxSum(array) {
    var sum = array[0],
        min = array[0],
        max = array[0];

    for (let i = 1; i < array.length; i++) {
        sum += array[i];
        if (min > array[i]) min = array[i];
        if (max < array[i]) max = array[i];
    }

    return [sum - max, sum - min];
}

console.log(minMaxSum([1, 3, 5, 7, 9]));
```
The solution is simple, just get the min and max value, sum the array and extract min or max in order to get the max sum or min sum.

As pseudocede:

```
min_value = find_min_value(arr)
max_value = find_max_value(arr)
max_sum = sum_array(arr) - min_value
min_sum = sum_array(arr) - max_value

```

:)