Easy understand fast java solution
Intuitionselect element as smallest or largest elementApproachselect each element, it can be largest or smallest, if largest, select k - 1 from its left, if sma
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
select element as smallest or largest element

# Approach
select each element, it can be largest or smallest, if largest, select k - 1 from its left, if smallest, select k - 1 from its right.
combination can be calculated with dynamic programming.

# Complexity
- Time complexity:
O(k * N)

- Space complexity:
O(k * N)

# Code
```java []
class Solution {    
    public int minMaxSums(int[] nums, int k) {
        long res = 0;
        Arrays.sort(nums);
        int mod = 1000000007;
        long[][] comb = new long[nums.length + 1][k + 1];
        for (int i = 0; i < comb.length; i++) {
            comb[i][0] = 1;
            if (i == 0) {
                continue;
            }
            for (int j = 1; j < comb[0].length; j++) {
                comb[i][j] = (comb[i - 1][j] + comb[i - 1][j - 1]) % mod;
            }
        }
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j <= Math.min(i, k - 1); j++) {
                res += comb[i][j] * nums[i];
                res = res % mod;
            }
            for (int j = 0; j <= Math.min(nums.length - i - 1, k - 1); j++) {
                res += comb[nums.length - i - 1][j] * nums[i];
                res = res % mod;
            }
        }
        return (int) (res % mod);
    }
}
```
['Java']