Number of Subsequences That Satisfy the Given Sum Condition - 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 in the subsequence is less than or equal to target.
Since the answer may be too large, return it modulo 109 + 7.
Note: A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [3,5,6,7], target = 9
›
Output:
4
💡 Note:
Valid subsequences: [3], [5], [6], [3,5]. Each has min + max ≤ 9. For [3]: 3+3=6≤9, [5]: 5+5=10>9 (invalid), [6]: 6+6=12>9 (invalid), [3,5]: 3+5=8≤9.
Example 2 — Smaller Target
$
Input:
nums = [3,3,6,8], target = 10
›
Output:
6
💡 Note:
After sorting [3,3,6,8]: valid subsequences include [3], [3], [3,3], [3,6], [3,6] (different 3's), [3,3,6]. Min + max ≤ 10 for each.
Example 3 — All Valid
$
Input:
nums = [2,3,3,4,6,7], target = 12
›
Output:
61
💡 Note:
Most subsequences are valid since target is large. Even [6,7] has 6+7=13>12, but many smaller combinations work.
Constraints
- 1 ≤ nums.length ≤ 105
- 1 ≤ nums[i] ≤ 106
- 1 ≤ target ≤ 106
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array [3,5,6,7] with target 9
2
Process
Check each subsequence's min + max against target
3
Output
Count of valid subsequences: 4
Key Takeaway
🎯 Key Insight: Sort first, then use two pointers to count 2^(right-left) subsequences efficiently
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code