↓Skip to main content
  1. LeetCode/

LeetCode 1498: Number of Subsequences That Satisfy the Given Sum Condition

·2 mins· ·
LeetCode Medium Two-Pointers Sorting Binary-Search Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-01-24

  • Submissions: Accepted ×2 → Wrong Answer → Accepted ×2

Approach
#

Whether a subsequence is valid depends only on minimum + maximum, not on the original order, so sort first.

Then use converging pointers:

  • nums[left] + nums[right] > target: nums[right] does not work even with the current smallest value, let alone larger ones, so drop it: right -= 1.
  • Otherwise, take nums[left] as the minimum. It must be chosen, and each of the right - left elements in left + 1..right is optional, with the maximum staying <= nums[right]: 2^(right - left) subsequences. Then left += 1.

Powers of two need the modulo: use pow(2, e, MOD) or precompute a table.

My Attempts
#

After the first pass I rewrote it a few times. One rewrite got WA: I switched to while ... else and the inner while had no left <= right guard, so right could drop below left. Then right - left is negative, and Python’s pow(2, -1, MOD) does not raise; it returns the modular inverse of 2, 500000004, which gets added to the answer.

For nums = [3, 5, 6, 7], target = 9, the answer is 4; this version returned 500000008.

Adding left <= right to the inner loop passed.

Solution
#

class Solution:
    def numSubseq(self, nums: List[int], target: int) -> int:
        MOD = 10 ** 9 + 7
        nums.sort()
        left, right = 0, len(nums) - 1
        ans = 0
        while left <= right:
            if nums[left] + nums[right] > target:
                right -= 1                         # no minimum works with nums[right]
            else:
                ans = (ans + pow(2, right - left, MOD)) % MOD
                left += 1                          # all subsequences with minimum nums[left] counted
        return ans

Complexity
#

Time O(n log n) (each pow is O(log n); a precomputed table makes it O(1)), space O(1) apart from sorting.

Mistakes to Avoid
#

  • Assuming subsequences forbid sorting: only the minimum and maximum matter here.
  • Letting right pass left: Python’s pow with a negative exponent and a modulus does not raise, it silently gives a wrong value.
  • It is 2^(right - left), not 2^(right - left + 1): the minimum nums[left] must be chosen.

Interview Explanation
#

Validity depends only on minimum plus maximum, so I sort. With converging pointers, if the two ends sum too high, I drop the right end; otherwise the left end is the minimum, it must be chosen, and the right - left elements between are free, giving 2^(right - left) subsequences, then the left end advances. Time O(n log n).

Related

LeetCode 825: Friends Of Appropriate Ages
·3 mins
LeetCode Medium Two-Pointers Sorting Binary-Search Array
LeetCode 825 note: after sorting, the people someone can invite have ages in (0.5 × age + 7, age], a contiguous range kept with two same-direction pointers, minus the person themself
LeetCode 923: 3Sum With Multiplicity
·4 mins
LeetCode Medium Two-Pointers Sorting Hash-Table Counting Array
LeetCode 923 note: sort, fix the first number, and find the other two with converging pointers; equal end values are counted with a combination, different ones by multiplying each end’s duplicate count
LeetCode 948: Bag of Tokens
·2 mins
LeetCode Medium Two-Pointers Greedy Sorting Array
LeetCode 948 note: after sorting, buy score with the cheapest token and buy power with the most expensive one; trade for power only when score > 0, and stop when neither move works
LeetCode 3413: Maximum Coins From K Consecutive Bags
·3 mins
LeetCode Medium Sliding-Window Greedy Sorting Prefix-Sum Binary-Search Array
LeetCode 3413 note: the best k bags always have one end on a segment boundary; compute “left end at a segment start” and “right end at a segment end” (the latter by mirroring and reusing the same function)
LeetCode 2779: Maximum Beauty of an Array After Applying Operation
·2 mins
LeetCode Medium Sliding-Window Sorting Binary-Search Array
LeetCode 2779 note: each number can become anything in [x - k, x + k], so a group can become one value ⇔ max - min <= 2k; sort and find the longest such window
LeetCode 611: Valid Triangle Number
·2 mins
LeetCode Medium Array Two-Pointers Binary-Search Greedy
LeetCode 611 note: sort, fix the longest side k, and use two pointers for the shorter sides; when the sum is large enough every i in between works, adding j - i at once