Combinatorics With Explanations
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
public:
    int MOD = 1e9+7;
    int minMaxSums(vector<int>& nums, int k) 
    {
        sort(nums.begin(), nums.end());
        int n = nums.size();
        vector<vector<int>> nCk(n+1, vector<int> (k+1, 0));
        // n(i) is the index, but k(j) is the length
        for (int i = 0; i <= n; i++)
        {
            for (int j = 0; j <= min(k, i); j++)
            {
                if (j == 0 || j == i) nCk[i][j] = 1; // nC0 = nCn = 1; i = 0 then j always = 0 -> i = j
                else nCk[i][j] = (nCk[i-1][j-1] + nCk[i-1][j]) % MOD; // nCk = (n-1)C(k-1) + (n-1)C(k);
            }
        }
        long long res = 0LL;
        for (int i = 0; i < n; i++)
        {
            long long tmp = 0LL;
        // Why i not i+1?
        // At index i, there are i+1 elements, but the subsequences we wanna collect must contains nums[i]
        //// so that we find: iCj instead of  (i+1)Cj
        // This also explanes for why not k but k-1: the final elements always nums[i], so we find k-1
            for (int j = 0; j <= min(i, k-1) ; j++)
            {
                tmp = (tmp + nCk[i][j]) % MOD;
            }
            res = (res + tmp*(nums[i] + nums[n-i-1])) % MOD;
        }
        return res;
    }
};

```
['C++']