combanitrics solution
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 minMaxSums(vector<int>& nums, int k) {
        // Sort the array to process elements in ascending order
        sort(nums.begin(), nums.end());
        int MOD = 1e9 + 7;
        int n = nums.size();
        
        // Precompute nCr (combinations) values using dynamic programming
        vector<vector<int>> ncr(n + 1, vector<int>(k + 1, 0));
        ncr[0][0] = 1; // Base case: C(0, 0) = 1
        for (int i = 1; i <= n; i++) {
            ncr[i][0] = 1; // C(n, 0) = 1 for all n
            for (int j = 1; j <= k; j++) {
                ncr[i][j] = (ncr[i - 1][j - 1] + ncr[i - 1][j]) % MOD; // Pascal's triangle formula
            }
        }

        long long ans = 0;

        // Calculate contribution of each element as the *minimum* value in subsequences
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < k; j++) {
                if (n - i - 1 >= j) { // Ensure there are enough elements to form a subsequence
                    ans = (ans + (long long)ncr[n - i - 1][j] * nums[i]) % MOD;
                }
            }
        }

        // Calculate contribution of each element as the *maximum* value in subsequences
        for (int i = n - 1; i >= 0; i--) {
            for (int j = 0; j < k; j++) {
                if (i >= j) { // Ensure there are enough elements to form a subsequence
                    ans = (ans + (long long)ncr[i][j] * nums[i]) % MOD;
                }
            }
        }

        return ans;
    }
};

```
['C++']