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]));
```
Here's the full code with more optimized and cleaned with all test cases working-
Using Arrays.sort()

```
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

// Complete the miniMaxSum function below.
   static void miniMaxSum(int[] arr) 
    {
       int min=0;
       int max=0;

       Arrays.sort(arr);

       for(int i=0; i<arr.length;i++)
       {
          if(i>0)
          {
             max+=arr[i];
          }
          if(i<4)
          {
            min+=arr[i];
          }
       }
      System.out.println(min + " " + max);
   }

   private static final Scanner scanner = new Scanner(System.in);

   public static void main(String[] args) 
   {
     int[] arr = new int[5];

     String[] arrItems = scanner.nextLine().split(" ");
     scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

     for (int i = 0; i < 5; i++) 
     {
        int arrItem = Integer.parseInt(arrItems[i]);
        arr[i] = arrItem;
     }

     miniMaxSum(arr);
     scanner.close();
  }
}

```