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 it is less than or equal to target. Since the answer may be too large, return it modulo 10^9 + 7.

30:00

Number of Subsequences That Satisfy the Given Sum Condition
medium
Free written solution
Topics
Companies

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 it is less than or equal to target. Since the answer may be too large, return it modulo 10^9 + 7.

Example 1:
Input: {"nums":[3,5,6,7],"target":9}
Output: 4
Constraints:
  • 1≤nums.length≤1051 \leq \text{nums.length} \leq 10^5

  • 1≤nums[i]≤1061 \leq \text{nums}[i] \leq 10^6

  • 1≤target≤1061 \leq \text{target} \leq 10^6

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[3,5,6,7],"target":9}

A subsequence qualifies when its smallest plus its largest value is at most 9. Order inside a subsequence does not matter for min and max, so sorting is safe.

nums, as given

need min + max <= 9

3
5
6
7
0
1
2
3

■ fixed min (always in)

■ free: in or out

- - max allowed for this min

result = 0

Variables
VariableValue
n4
target9
DepthFunction Call
Stack empty
0/12