Skip to content
Decoder

Number of Subsequences That Satisfy the Given Sum Condition

Medium

Problem

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`.

Example 1

Input
nums = [3, 5, 6, 7], target = 9
Output
4
Why
[3], [3, 5], [3, 5, 6], [3, 6]

Example 2

Input
nums = [3, 3, 6, 8], target = 10
Output
6

Example 3

Input
nums = [5], target = 9
Output
0
Why
5 + 5 = 10 is too big

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.