Easy Optimized App | Get the  Intuition |  💫
Intuition (VVI)
Here we do have to notice that order of the elements does not matter, only the thing that matters most is the elements we will choose, as among
# Intuition (VVI)
<!-- Describe your first thoughts on how to solve this problem. -->
1. Here we do have to notice that order of the elements does not matter, only the thing that matters most is the elements we will choose, as among those elements we will get minm and maxm.
2. If we sort the array, then it will make thing little easy. Lets say after sorting we are standing on index i now we can calculate the no of subsequence start from that index (here that element will act as minimum) and no of subsequence ends at that index (here that element will act as maxmum).
3. Again when we get a subsequence with a single element lets say [2]
then 2 itself is maximum as well as minimum.

# Approach
- See Inutuion First !!!!
<!-- Describe your approach to solving the problem. -->
1. First create a dp vector to preCalculate nCr with maxsize of n+1 and k + 1.
2. Sort the nums array. Intialize ans with 0.
3. Iterate through index 0 to n-1.
4. Now remaining element at right side will be n-1-i. Intialize a temporary count of 0.
5. Create an inner loop from sequence length 1 to k.
6. If seq length is 1 then no more element can be selected other than current index i, so just add 2 to the temp as current will act as both maxm and minm. If length is gretaer than 1 then count no of possible cominations by getting value of xCy where x is no of remaining elements in right side of i and y is length-1. Add that value to temp. If length-1 is gretaer than remaing count then break inner loop.
7. So temp value will indicate no of times nums[i] will acts as minimum(but one count is for it acting as maximum if sequence length is 1 like [2]). Multiple temp value by nums[i] and add to ans.
8. After finshing, Similarly do this from  index n-1 to i. One thing that here we dont count for length = 1, as if we count here it will be counted double times.
9. Return ans.

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

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

# Code
```cpp []
class Solution {
public:
    int mod = 1e9 + 7;
    int minMaxSums(vector<int>& nums, int k) {
        int n = nums.size();
        sort(nums.begin(), nums.end());
        vector<vector<int>>C(n+1, vector<int>(k+1, 0));
        C[0][0] = 1;
        for(long long i = 1; i <= n; i++){
            C[i][0] = 1;
            int minm = k;
            if(i < k) minm = i;
            for(long long length = 1; length <= minm; length++){
                C[i][length] = ((long long)C[i-1][length-1] + (long long)C[i-1][length])%mod;
            }
        }

        long long ans = 0;
        for(int i = 0; i < n; i++){
            long long tem = 0;
            int rem_length = (n-1-i);
            for(int length = 1; length <= k; length++){
                if(length == 1) tem = (tem + 2)%mod;
                if(length > 1) tem = (tem + C[rem_length][length-1])%mod;
                if(rem_length < length-1) break;
            }
            tem = (tem * nums[i])%mod;
            ans = (ans + tem)%mod;
        }

        for(int i = n-1; i >= 0; i--){
            long long tem = 0;
            int rem_length = i;
            for(int length = 1; length <= k; length++){
                if(length > 1) tem = (tem + C[rem_length][length-1])%mod;
                if(rem_length < length-1) break;
            }
            tem = (tem * nums[i])%mod;
            ans = (ans + tem)%mod;
        }
        return ans;
    }
};
```
['Array', 'Dynamic Programming', 'C++']