C++ simple
IntuitionThe task is to calculate the sum of the maximum and minimum elements of all subsequences of the array with at most k elements. Since the number of subs
# Intuition
The task is to calculate the sum of the maximum and minimum elements of all subsequences of the array with at most k elements. Since the number of subsequences can be very large, direct computation isn't feasible. The problem can be simplified using combinatorics and modular arithmetic:

Sort the array to easily pick the smallest and largest elements for subsequences.
Use combinations (nCr) to calculate how many times each element contributes as the smallest or largest element in valid subsequences.
Add these contributions to the final result using modular arithmetic to handle large values.

# Approach
Sort the Array:
Sorting helps to directly identify the smallest (nums[i]) and largest (nums[n - i - 1]) elements for subsequences.

Combination Sum Calculation:
Use a helper function find_NCR_series_sum to calculate the sum of combinations for all valid subsequences of size ≤ k.

Use Fermat's Little Theorem to compute modular inverse for nCr efficiently.
Accumulate the sum of combinations up to min(k-1, m) for each element.
Iterate Over the Array:
For each element nums[i], calculate its contribution as the smallest and largest element in subsequences. The contribution is weighted by the sum of valid combinations calculated earlier.





# Complexity
- Time complexity:

Sorting: 

O(nlogn)

Combination Calculations:
O(n⋅k).

Overall time complexity:
O(nlogn+n⋅k)

- Space complexity:
O(1)

# Code
```cpp []
class Solution {
public:
    typedef long long ll;
    ll mod = 1e9 + 7;
    ll modinverse(ll p, ll q) // (p/q)%mod fermat's little theorem
    {
        ll expo = mod-2;

        while(expo)
        {
           if(expo & 1)
           {
             p = (p*q)%mod;
           }

           q = (q*q) % mod;
           expo = expo/2;
        }

        return p;
    }
    ll find_NCR_series_sum(ll n,ll r)
    {
       ll sum = 1;
       ll nci = 1;

       for(ll i = 1 ; i <=r; i++)
       {
         ll mii = modinverse(n-i+1,i);
         nci = (nci*mii)%mod;
         sum  = (sum + nci)%mod;
       }

        return sum;
    }
    int minMaxSums(vector<int>& nums, ll k) {
        int n = nums.size();
        sort(nums.begin(), nums.end());
        
        ll ans = 0;

        for(int i=0;i<n;i++)
        {
            ll m = n - i - 1;
            ll r = min (k-1 ,m);
            ans = (ans + ((nums[i] + nums[n-i-1])*find_NCR_series_sum(m,r))%mod)%mod;
        }

        return ans;   
    }
};
```
['C++']