Number of Subsequences That Satisfy the Given Sum Condition

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 in the subsequence is less than or equal to target.

Since the answer may be too large, return it modulo 10^9 + 7.

Example 1
Inputnums = [3,5,6,7], target = 9
Output4
There are 4 valid subsequences: [3], [3,5], [3,5,6], and [3,6].
Example 2
Inputnums = [3,3,6,8], target = 10
Output6
There are 6 valid subsequences, counting repeated numbers as distinct positions: [3], [3], [3,3], [3,6], [3,6], and [3,3,6].

Constraints

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

Asked at 6 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate