Detailed Solution for Java Guys 📝
Problem: Sum of Min-Max of At Most k SubsequencesIntuitionThe problem requires calculating the sum of the minimum and maximum values for all subsequences of len
# Problem: Sum of Min-Max of At Most k Subsequences

## Intuition
The problem requires calculating the sum of the minimum and maximum values for all subsequences of length at most k. Explicitly generating all subsequences is computationally expensive, especially for large arrays. Instead, we leverage **combinatorics** to count subsequences efficiently. The key insight is that:

- Each element in the array contributes as the **minimum** or **maximum** value in multiple subsequences.
- Sorting the array allows us to process these contributions systematically, as the minimum or maximum in a subsequence depends on the relative position of the element.

By precomputing factorials and modular inverses, we efficiently calculate binomial coefficients (C(n,r)) to determine how many subsequences include each element as the smallest or largest.

---

## Approach

### 1. Sorting the Array
- First, sort the array in ascending order. This makes it easy to calculate contributions when an element is the smallest (consider elements to the **right**) or the largest (consider elements to the **left**).

---

### 2. Precomputing Factorials and Modular Inverses
- To efficiently compute C(n,r), precompute:
  - Factorials fact[i] modulo 10^9 + 7 for i = 0, 1, ..., n
  - Modular inverses inv[i] of factorials using Fermat's Little Theorem:
    ```
    inv[i] = fact[i]^(mod-2) mod (10^9 + 7)
    ```
- These allow us to compute C(n,r) in O(1) time after O(n) preprocessing.

---

### 3. Calculating Contributions for Each Element
For each nums[i] in the sorted array:

#### Minimum Contribution
- nums[i] acts as the **smallest** element in subsequences formed using elements **to the right of nums[i]** (including itself).
- The number of subsequences is determined by selecting up to k elements from the (n-i-1) elements on the right:
  ```
  minContribution = nums[i] × ∑(j=0 to min(k,n-i-1)) C(n-i-1,j)
  ```

#### Maximum Contribution
- nums[i] acts as the **largest** element in subsequences formed using elements **to the left of nums[i]** (including itself).
- The number of subsequences is determined by selecting up to k elements from the i elements on the left:
  ```
  maxContribution = nums[i] × ∑(j=0 to min(k,i)) C(i,j)
  ```

---

### 4. Combining Results
- Add the minimum and maximum contributions for each element to a cumulative sum modulo 10^9 + 7:
  ```
  totalSum = (totalSum + minContribution + maxContribution) mod (10^9 + 7)
  ```

---

### 5. Return the Result
After processing all elements, return the total sum modulo 10^9 + 7.

---

## Complexity Analysis

### Time Complexity
1. **Sorting**: O(n log n) for sorting the array.
2. **Precomputations**: O(n) for factorials and modular inverses.
3. **Main Loop**: O(n × k), where k is the maximum subsequence size.

**Overall**: O(n log n + n × k).

---

### Space Complexity
- O(n) for storing factorials and inverses.

**Overall**: O(n).


# Code
```java []
class Solution {
    static final int mod = (int) 1e9 + 7;
    static long[] fact;
    static long[] inv;
    
    static void precomputeFactorials(int n) {
        fact = new long[n + 1];
        inv = new long[n + 1];
        fact[0] = 1;
        for (int i = 1; i <= n; i++) {
            fact[i] = (fact[i - 1] * i) % mod;
        }
        inv[n] = power(fact[n], mod - 2, mod);
        for (int i = n - 1; i >= 0; i--) {
            inv[i] = (inv[i + 1] * (i + 1)) % mod;
        }
    }
    
    static long power(long a, long b, int m) {
        long val = 1;
        a = a % m;
        while (b > 0) {
            if ((b & 1) == 1) val = (val * a) % m;
            b = b >> 1;
            a = (a * a) % m;
        }
        return val;
    }
    
    static long nCr(int n, int r) {
        if (r < 0 || r > n) return 0;
        return (fact[n] * inv[r] % mod * inv[n - r]) % mod;
    }
    
    public int minMaxSums(int[] nums, int k) {
        int n = nums.length;
        Arrays.sort(nums);
        precomputeFactorials(n);
        
        long sum = 0;
        for (int i = 0; i < n; i++) {
            long val = 0;
            int a = i;
            int b = n - 1 - i;
            
            for (int j = 0; j < k && j <= a; j++) {
                val = (val + nCr(a, j)) % mod;
            }
            sum = (sum + (val * nums[i]) % mod) % mod;
            
            val = 0;
            for (int j = 0; j < k && j <= b; j++) {
                val = (val + nCr(b, j)) % mod;
            }
            sum = (sum + (val * nums[i]) % mod) % mod;
        }
        
        return (int) sum;
    }
}
```
['Java']