> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Number of Subsequences That Satisfy the Given

> Tested Python solution for LeetCode 1498 with 22 pytest cases. Generate a practice environment with lcpy.

LeetCode 1498, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Two Pointers](/catalog/topics/two-pointers), [Binary Search](/catalog/topics/binary-search), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/number-of-subsequences-that-satisfy-the-given-sum-condition/description/).

Generate this problem as a practice environment: tested reference solution, 22 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1498   # by problem number
lcpy gen -s number_of_subsequences_that_satisfy_the_given_sum_condition   # by problem name
```

## Problem

You are given an array of integers `nums` and an integer `target`.

Return *the number of **non-empty** subsequences of* `nums` *such that the sum of the minimum and maximum element on it is less or equal to* `target`. Since the answer may be too large, return it **modulo** `10^9 + 7`.

### Examples

```
Input: nums = [3,5,6,7], target = 9
Output: 4
Explanation: There are 4 subsequences that satisfy the condition.
[3] -> Min value + max value <= target (3 + 3 <= 9)
[3,5] -> (3 + 5 <= 9)
[3,5,6] -> (3 + 6 <= 9)
[3,6] -> (3 + 6 <= 9)
```

```
Input: nums = [3,3,6,8], target = 10
Output: 6
Explanation: There are 6 subsequences that satisfy the condition. (nums can have repeated numbers).
[3] , [3] , [3,3], [3,6] , [3,6] , [3,3,6]
```

```
Input: nums = [2,3,3,4,6,7], target = 12
Output: 61
Explanation: There are 63 non-empty subsequences, two of them do not satisfy the condition ([6,7], [7]).
Number of valid subsequences (63 - 2 = 61).
```

### Constraints

* 1 \<= nums.length \<= 10^5
* 1 \<= nums\[i] \<= 10^6
* 1 \<= target \<= 10^6

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_subsequences_that_satisfy_the_given_sum_condition/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_subsequences_that_satisfy_the_given_sum_condition/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n log n)
    # Space: O(1) auxiliary (sorting aside)
    def num_subseq(self, nums: list[int], target: int) -> int:
        nums = sorted(nums)
        mod = 1_000_000_007
        n = len(nums)
        pows = [1] * n
        for i in range(1, n):
            pows[i] = pows[i - 1] * 2 % mod

        result = 0
        left, right = 0, n - 1
        while left <= right:
            if nums[left] + nums[right] <= target:
                result = (result + pows[right - left]) % mod
                left += 1
            else:
                right -= 1
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(n log n) | O(1) auxiliary (sorting aside) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.