combinatorics with simple explanation.
IntuitionApproachComplexity

Time complexity:
O(n.k)


Space complexity:
O(n.k)

Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

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

- Space complexity:
O(n.k)

# Code
```cpp []
class Solution {
public:
    //the problem at hand is to count the number of times each element can be the maximum and minimum
    //nums[i] is maximum for nums[0] to nums[i]
    //nums[i] is minimum for nums[i] to nums[n-1]
    // the number of times nums[i] is max and min is equal to number of times nums[i] is max and number of times nums[n-1] is max(the number of times nums[n-1] is max will be the same as number of times nums[i] is minimum)
    //thus count_of_nums[i] = max_times_nums[i] + max_times_nums[n-1] = max_times_nums[i] + min_times_nums[i];
    int mod = (int)(1e9 + 7);
    int minMaxSums(vector<int>& nums, int k) {
        sort(nums.begin(), nums.end());
        int n = nums.size();
        //using pascals triangle
        //C(i,j) = C(i-1,j-1) + C(i-1,j)
        vector<vector<int>> comb(n, vector<int>(k, 0));
        comb[0][0] = 1;
        for (int i = 1; i < n; i++) {
            comb[i][0] = 1;
            for (int j = 1; j < k; j++)
                comb[i][j] = ((comb[i - 1][j - 1]) + (comb[i - 1][j])) % mod;
        }
        int ans = 0;
        for (int i = 0; i < n; i++) {
            long long count = 0;
            for (int j = 0; j < k && j <= i; j++)
                count = (count + comb[i][j]) % mod;
            ans = (ans + (count * (nums[i] + nums[n - i - 1]))) % mod;
        }
        return ans;
    }
};
```
['C++']