Clear Explanation | Java | Beats 100% space and time
Intuition
We don't need to find the sum (max and min) for each subsequence individually or in group
Rather find the minimum elements sum and maximum elements su
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
1. We don't need to find the sum (max and min) for each subsequence individually or in group
2. Rather find the minimum elements sum and maximum elements sum in each subsequence, separately and combine
3. As the question asked for the subsequences, we can sort the array as it is easy to work with max and min
4. In a sorted array, for `nums[i]` to be a maximum of the subsequences, all the elements in those subsequences to be choosen from the `nums[0...i-1]`. Similarly, for `nums[i]` to be minimum, all other elements should be choosen from `nums[i+1...n-1]`;

# Approach
<!-- Describe your approach to solving the problem. -->
1. In order to find just the subsequences with length `l` we can using combinations $$\binom{N}{k}$$. And for at most k, it will be $$\sum_{l=1}^{K}\binom{N}{l}$$

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

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

# Code
```java []
class Solution {
    public int minMaxSums(int[] nums, int k) {
        int n = nums.length;
        int[][] ncr = new int[n+1][k+1];
        int p = (int)1e9+7;
        
        ncr[0][0] = 1;
        ncr[0][1] = 0;

        for(int i = 1;i <= n;i++) {
            ncr[i][0] = 1;
            for(int j = 1;j<=Math.min(i, k);j++) {
                long temp = 0L + ncr[i-1][j-1] + ncr[i-1][j];
                ncr[i][j] = (int) (temp%p);
            }
        }
        Arrays.sort(nums);
        long ans = 0;
        for(int i = 0;i<n;i++) {
            // count times when nums[i] is maximum, by selecting elements from [0...i-1];
            int leftElements = i;

            for(int j = 0;j<= Math.min(leftElements, k-1);j++) {
                ans += 1l*(ncr[leftElements][j])*nums[i];
                ans %= p;
            }

            // count time when nums[i] is minimum, by selecting elements from [i+1...n-1];
            int rightElements = n-i-1;
            for(int j = 0;j<= Math.min(rightElements, k-1);j++) {
                ans += 1l*(ncr[rightElements][j])*nums[i];
                ans %= p;
            }
        }

        return (int)ans%p;
    }
}
```
['Combinatorics', 'Java']