πΉ Problem: 1498. Number of Subsequences That Satisfy the Given Sum Condition
Difficulty: #Medium
Tags: #TwoPointers, #Greedy, #Sorting, #BinarySearch, #Combinatorics
π Problem Summary
Given a list of integers
nums
and an integertarget
, count the number of non-empty subsequences such that the sum of the minimum and maximum elements in the subsequence is less than or equal to the target.
Subsequences can be in any order and we care about the count, not the actual lists. Return the count modulo 10^9 + 7
.
π§ My Thought Process
Brute Force Idea:
I could technically generate all subsequences using backtracking or DFS, check their min + max, and count the valid ones. But, let's not... O(2^n) is a death sentence for even moderately-sized inputs.Optimized Strategy:
This is where the fun began β I knew sorting would help (yay!), and I vaguely remembered something about using two pointers when dealing with subsequences and boundaries.
The idea is:
- Sort the array so that min/max pairs are easier to manage.
- Fix the smallest element using a left pointer.
- Then, use a right pointer to find the furthest index where
nums[left] + nums[right] <= target
. - The number of valid subsequences in that range is
2^(right - left)
(include/exclude middle elements).
Now, did I know this formula off the top of my head?
Of course not.
Did I panic and look it up?
Absolutely.
Combinatorics still haunts me in my dreams.
βοΈ Code Implementation (Python)
class Solution:
def numSubseq(self, nums, target):
mod = 10**9 + 7
nums.sort()
n = len(nums)
power = [1] * n
for i in range(1, n):
power[i] = (power[i - 1] * 2) % mod
left, right = 0, n - 1
result = 0
while left <= right:
if nums[left] + nums[right] <= target:
result = (result + power[right - left]) % mod
left += 1
else:
right -= 1
return result
β±οΈ Time & Space Complexity
- Time: O(n log n) β Sorting dominates. The two-pointer scan is O(n).
-
Space: O(n) β For the
power
table storing2^i mod MOD
.
π§© Key Takeaways
- β When you see βcount subsequences that satisfy a boundary conditionβ, you might not need to generate them all β just count them using math.
- π‘ Use sorting + two pointers when min/max or range comparisons are involved.
- π Precomputing
2^i
is a common trick when you want to count subsets.
π Reflection (Self-Check)
[β] Could I solve this without help?
Haha. No. I had the right direction and then immediately crashed into a wall of fear and bitmask trauma.[β] Did I write code from scratch?
I modified and adapted what I found after shamelessly googling it.[β ] Did I understand why it works?
After several hours of emotional support and revisiting middle school combinatorics? Yes.[β ] Will I be able to recall this in a week?
Only if I tattoo2^(right - left)
onto my forearm.
π Related Problems
[[2835 Minimum Operations to Form Subsequence With Target Sum]]
[[3098 Find the Sum of Subsequence Powers]]
[[3082 Find the Sum of the Power of All Subsequences]]
π Progress Tracker
Metric | Value |
---|---|
Day | 34 |
Total Problems Solved | 367 |
Confidence Today | π£ |
Top comments (0)