You're given an array of integers nums and an integer target. Your task is to find the number of non-empty subsequences where the sum of the minimum and maximum elements is less than or equal to the target.
A subsequence is derived from the original array by deleting some or no elements without changing the order of the remaining elements. For example, from [3,6,7,7], some valid subsequences are [3,7,7], [6], or [3,6,7].
Key insight: We only care about the min and max values in each subsequence, not the sum of all elements! This makes the problem much more manageable.
Since the answer can be very large, return the result modulo 109 + 7.
Input & Output
Visualization
Time & Space Complexity
We generate 2^n subsequences and spend O(k) time on each subsequence of length k
Recursion stack depth or space to store current subsequence
Constraints
- 1 โค nums.length โค 105
- 1 โค nums[i] โค 106
- 1 โค target โค 106
- Return result modulo 109 + 7