Number of Subsequences That Satisfy the Given Sum Condition - Problem

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

example_1.py โ€” Basic case
$ Input: nums = [3,5,6,7], target = 9
โ€บ Output: 4
๐Ÿ’ก Note: There are 4 valid subsequences: [3], [3,5], [3,5,6], [3,6]. For [3]: min+max = 3+3 = 6 โ‰ค 9. For [3,5]: min+max = 3+5 = 8 โ‰ค 9. For [3,5,6]: min+max = 3+6 = 9 โ‰ค 9. For [3,6]: min+max = 3+6 = 9 โ‰ค 9.
example_2.py โ€” Edge case
$ Input: nums = [3,3,6,8], target = 10
โ€บ Output: 6
๐Ÿ’ก Note: Valid subsequences are [3], [3] (second occurrence), [3,3], [3,6], [3,6] (with second 3), [3,3,6]. All have min+max โ‰ค 10.
example_3.py โ€” No valid subsequences
$ Input: nums = [2,3,3,4,6,7], target = 12
โ€บ Output: 61
๐Ÿ’ก Note: Many subsequences are valid since even the largest elements sum to manageable values. The two-pointer approach efficiently counts all valid combinations.

Visualization

Tap to expand
Team Selection StrategyBudget: 9 skill points (min + max players)Players sorted by skill level:3Weakest567StrongestStep 1: Check weakest (3) + strongest (7) = 10 > 9 โŒStep 2: Check weakest (3) + next strongest (6) = 9 โ‰ค 9 โœ…Valid range: players 3, 5, 6Teams with player 3: [3], [3,5], [3,6], [3,5,6]Count = 2^(2-0) = 4 teams๐ŸŽฏ Key Insight: Sorting + Two Pointers = O(n log n) instead of O(2^n)Mathematical insight: 2^(right-left) counts all valid subsequences in range
Understanding the Visualization
1
Line up by skill
Sort all players by their skill level (sort the array)
2
Check extreme players
Look at weakest available player (left) and strongest candidate (right)
3
Count valid teams
If weakest + strongest โ‰ค budget, all teams with weakest player and anyone from left to right work
4
Move strategically
If valid, move to next weakest player; if not, reduce strongest candidate
Key Takeaway
๐ŸŽฏ Key Insight: By sorting first, we can use two pointers to efficiently count valid subsequences. When nums[left] + nums[right] โ‰ค target, all 2^(right-left) subsequences starting with nums[left] are valid, allowing us to count exponentially many subsequences in constant time.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(2^n)

We generate 2^n subsequences and spend O(k) time on each subsequence of length k

n
2n
โš  Quadratic Growth
Space Complexity
O(n)

Recursion stack depth or space to store current subsequence

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • 1 โ‰ค nums[i] โ‰ค 106
  • 1 โ‰ค target โ‰ค 106
  • Return result modulo 109 + 7
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
42.5K Views
Medium-High Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen