Combinatorics Easy Solution🔥😍| Optimized Code⚡⚡
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 []
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define MOD 1000000007

ll binExpo(ll base, ll exp, ll mod) {
    ll result = 1;
    base %= mod;
    while (exp > 0) {
        if (exp & 1) result = (result * base) % mod;
        base = (base * base) % mod;
        exp >>= 1;
    }
    return result;
}

ll modInverse(ll a, ll mod) {
    return binExpo(a, mod - 2, mod);
}

void precomputeFactorials(vector<ll>& fact, vector<ll>& invFact, int n, ll mod) {
    fact[0] = 1;
    for (int i = 1; i <= n; i++) {
        fact[i] = (fact[i - 1] * i) % mod;
    }
    invFact[n] = modInverse(fact[n], mod);
    for (int i = n - 1; i >= 0; i--) {
        invFact[i] = (invFact[i + 1] * (i + 1)) % mod;
    }
}

ll nCr(int n, int r, vector<ll>& fact, vector<ll>& invFact, ll mod) {
    if (r > n || r < 0) return 0;
    return (fact[n] * invFact[r] % mod * invFact[n - r] % mod) % mod;
}

class Solution {
public:
    int minMaxSums(vector<int>& nums, int k) {
        int n = nums.size();
        sort(nums.begin(), nums.end());
        ll ans = 0;
        vector<ll> fact(n + 1), invFact(n + 1);
        precomputeFactorials(fact, invFact, n, MOD);
        for (int i = 0; i < n; i++) {
            for (int x = 0; x < k && x <= n - i - 1; x++) {
                ans = (ans + nums[i] * nCr(n - i - 1, x, fact, invFact, MOD)) % MOD;
            }
        }
        for (int i = n - 1; i >= 0; i--) {
            for (int x = 0; x < k && x <= i; x++) {
                ans = (ans + nums[i] * nCr(i, x, fact, invFact, MOD)) % MOD;
            }
        }
        return ans;
    }
};

```
['C++']