C++ solution | Easy to understand | Combinatorics | Clean | Simple | Commented
IntuitionTry to think of how does a single element make an impact on the total sum that is going to be created. You will notice that for an element to add its v
# Intuition
Try to think of how does a single element make an impact on the total sum that is going to be created. You will notice that for an element to add its value to the total sum, it either has to be minimum or maximum in a given subsequence.

# Approach
Now to find the total number of subsequence that can be made where ith element is minimum & ith element is maximum can't be found easily.
Sorting the array will solve this problem for us. Now for ith element we know how many elements exist that are greater than the current number & smaller than the current number.
Now we just have to choose different sizes of subsequences starting from 1 to K such that our current number contributes as minimum or maximum in the subsequence. And while you choose different sizes of subsequence u also have to freedom to choose any numbers u want in the subsequence therefore we use nCr here to find total different choices that we have to create a subsequence of certain size.

Go through the code and the comments to understand further.

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

- Space complexity:
O(n*k)

# Code
```cpp []
class Solution {
public:
    int minMaxSums(vector<int>& nums, int k) {
        int n = nums.size();
        int mod = 1e9 + 7;
        // Method to store all nCr values in a table by precomputation, 
        // where dp[i][j] = iCj % mod 
        vector<vector<long long>> dp(n + 1, vector<long long>(101, 0));
        for (int i = 0; i <= n; i++) {
            dp[i][0] = 1;
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= 100; j++) {
                dp[i][j] = (dp[i - 1][j] % mod + dp[i - 1][j - 1] % mod) % mod;
            }
        }
        long long ans = 0;
        // Sort the array because the order of the numbers doesnt matter as 
        // we only count the contrubution by a number as a minimum & maximum number.
        sort(nums.begin(), nums.end());
        for (int i = 0; i < n; i++) {
            for (int sz = 1; sz <= k; sz++) {
                // mn is the total contribution of ith element as a minimum number 
                // in a subsequence of size 'sz'.
                long long mn = (nums[i] * dp[n - i - 1][sz - 1]) % mod;
                // mx is the total contribution of ith element as a maximum number 
                // in a subsequence of size 'sz'.
                long long mx = (nums[i] * dp[i][sz - 1]) % mod;
                // finally all the contribution of each element adds up to the total sum
                ans = ((ans % mod) + (mx + mn)) % mod;
            }
        }
        return ans;
    }
};
```
['C++']