Class Solution

  • All Implemented Interfaces:

    
    public final class Solution
    
                        

    3428 - Maximum and Minimum Sums of at Most Size K Subsequences.

    Medium

    You are given an integer array nums and a positive integer k. Return the sum of the maximum and minimum elements of all

    subsequences

    of nums with at most k elements.

    Since the answer may be very large, return it modulo <code>10<sup>9</sup> + 7</code>.

    Example 1:

    Input: nums = 1,2,3, k = 2

    Output: 24

    Explanation:

    The subsequences of nums with at most 2 elements are:

    The output would be 24.

    Example 2:

    Input: nums = 5,0,6, k = 1

    Output: 22

    Explanation:

    For subsequences with exactly 1 element, the minimum and maximum values are the element itself. Therefore, the total is 5 + 5 + 0 + 0 + 6 + 6 = 22.

    Example 3:

    Input: nums = 1,1,1, k = 2

    Output: 12

    Explanation:

    The subsequences [1, 1] and [1] each appear 3 times. For all of them, the minimum and maximum are both 1. Thus, the total is 12.

    Constraints:

    • <code>1 <= nums.length <= 10<sup>5</sup></code>

    • <code>0 <= numsi<= 10<sup>9</sup></code>

    • 1 &lt;= k &lt;= min(70, nums.length)

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      Solution()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final Integer minMaxSums(IntArray nums, Integer k)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait