O(n ) solutions....take prefix of nCr table rows
Complexity
Time complexity:O(nlogn)

Space complexity:O(n*k)
Code


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

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

# Code
```cpp []
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#define ll long long

const int MAX_N = 100001;
const int MAX_K = 101;
ll MOD = 1e9 + 7;
ll mod=1e9+7;

ll nCr[MAX_N][MAX_K];
bool nCrInitialized = false;

void precomputeNCR() {
    if (nCrInitialized) return;
    memset(nCr, 0, sizeof(nCr));
    for (int i = 0; i < MAX_N; i++) {
        nCr[i][0] = 1;
        for (int j = 1; j <= min(i, MAX_K - 1); j++) {
            nCr[i][j] = (nCr[i - 1][j] + nCr[i - 1][j - 1]) % MOD;
        }
    }

    for (int i = 0; i < MAX_N; i++) {
       // nCr[i][0] = 1;
        for (int j = 1; j <MAX_K; j++) {
            nCr[i][j] = (nCr[i][j] + nCr[i][j - 1]) % MOD;
        }
    }
    nCrInitialized = true;
}
class Solution {
public:
    int minMaxSums(vector<int>& nums, int k) {
        precomputeNCR();
        
        sort(nums.begin(),nums.end());
         ll ans=0;
       int n=nums.size();
      
        for(int j=0;j<n;j++){
           ans=(ans%mod + (nCr[j][k-1]*nums[j])%mod+(nCr[n-j-1][k-1]*nums[j])%mod)%mod;
        }
      

        return ans;
    }
};
```
['Array', 'Math', 'Dynamic Programming', 'Sorting', 'Combinatorics', 'Prefix Sum', 'C++']