 🔢 Min-Max Sums Problem
📤 Share
Share this solution to help others.
👍 Vote
Vote for the best solution to make it stand out!
🤔Intuition
Sort the array, use combinatorics to calculate th
# 📤 Share

- Share this solution to help others.
 # 👍 Vote

- Vote for the best solution to make it stand out!
#  🤔Intuition
- Sort the array, use combinatorics to calculate the number of subsequences, and apply the formula for sum calculation.
<!-- Describe your first thoughts on how to solve this problem. -->

#  🔍 Approach
- Sort the array, compute binomial coefficients (nCr), and calculate min-max sums for each subsequence using the formula.
<!-- Describe your approach to solving the problem. -->

# 🧮 Complexity
- ⏱️Time complexity: 𝑂(𝑛2)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

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

# 💻Code
```cpp []
class Solution {
public:
    int minMaxSums(vector<int>& nums, int k) {
        sort(nums.begin(), nums.end());
        int ans = 0;
        int MOD = 1e9 + 7;
        vector<vector<int>> ncr(nums.size() + 1, vector<int> (k + 1,0));
        
        ncr[0][0] = 1;
        for (int n = 1; n <= nums.size(); n++) {
            ncr[n][0] = 1;
            for (int r = 1; r <= k; r++) 
                ncr[n][r] = (ncr[n - 1][r - 1] + ncr[n - 1][r]) % MOD;
        } 

        for(int n = 0; n < nums.size(); n ++) {
            int numberOfSubsequences = 0;
            for(int r = 0; r <= k - 1; r ++) 
                if(n >= r) numberOfSubsequences = (numberOfSubsequences + ncr[n][r]) % MOD;
            ans = (ans + (long long)(nums[n] + nums[nums.size() - n - 1]) * numberOfSubsequences) % MOD;
        }
        return ans;
    }
};
```
['C++']