Skip to content

Number of Subsequences That Satisfy the Given Sum Condition

MediumTwo Pointers
Decode it first. Clues, constraints, pattern and plan before any code.

Given a list of integers nums and an integer target, count the non-empty subsequences whose minimum plus maximum is at most target. A subsequence picks any set of positions (not necessarily next to each other). The count can be huge, so return it modulo 10^9 + 7.

nums = [3, 5, 6, 7], target = 9   →  4   ([3], [3, 5], [3, 5, 6], [3, 6])
nums = [3, 3, 6, 8], target = 10  →  6
nums = [5], target = 9            →  0   (5 + 5 = 10 is too big)

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^6
  • 1 <= target <= 10^6