|
| 1 | +# 1498. Number of Subsequences That Satisfy the Given Sum Condition |
| 2 | + |
| 3 | +- Difficulty: Medium. |
| 4 | +- Related Topics: Array, Two Pointers, Binary Search, Sorting. |
| 5 | +- Similar Questions: . |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +You are given an array of integers `nums` and an integer `target`. |
| 10 | + |
| 11 | +Return **the number of **non-empty** subsequences of **`nums`** such that the sum of the minimum and maximum element on it is less or equal to **`target`. Since the answer may be too large, return it **modulo** `109 + 7`. |
| 12 | + |
| 13 | + |
| 14 | +Example 1: |
| 15 | + |
| 16 | +``` |
| 17 | +Input: nums = [3,5,6,7], target = 9 |
| 18 | +Output: 4 |
| 19 | +Explanation: There are 4 subsequences that satisfy the condition. |
| 20 | +[3] -> Min value + max value <= target (3 + 3 <= 9) |
| 21 | +[3,5] -> (3 + 5 <= 9) |
| 22 | +[3,5,6] -> (3 + 6 <= 9) |
| 23 | +[3,6] -> (3 + 6 <= 9) |
| 24 | +``` |
| 25 | + |
| 26 | +Example 2: |
| 27 | + |
| 28 | +``` |
| 29 | +Input: nums = [3,3,6,8], target = 10 |
| 30 | +Output: 6 |
| 31 | +Explanation: There are 6 subsequences that satisfy the condition. (nums can have repeated numbers). |
| 32 | +[3] , [3] , [3,3], [3,6] , [3,6] , [3,3,6] |
| 33 | +``` |
| 34 | + |
| 35 | +Example 3: |
| 36 | + |
| 37 | +``` |
| 38 | +Input: nums = [2,3,3,4,6,7], target = 12 |
| 39 | +Output: 61 |
| 40 | +Explanation: There are 63 non-empty subsequences, two of them do not satisfy the condition ([6,7], [7]). |
| 41 | +Number of valid subsequences (63 - 2 = 61). |
| 42 | +``` |
| 43 | + |
| 44 | + |
| 45 | +**Constraints:** |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +- `1 <= nums.length <= 105` |
| 50 | + |
| 51 | +- `1 <= nums[i] <= 106` |
| 52 | + |
| 53 | +- `1 <= target <= 106` |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +## Solution |
| 58 | + |
| 59 | +```javascript |
| 60 | +/** |
| 61 | + * @param {number[]} nums |
| 62 | + * @param {number} target |
| 63 | + * @return {number} |
| 64 | + */ |
| 65 | +var numSubseq = function(nums, target) { |
| 66 | + nums.sort((a, b) => a - b); |
| 67 | + var res = 0; |
| 68 | + var l = 0; |
| 69 | + var r = nums.length - 1; |
| 70 | + var mod = Math.pow(10, 9) + 7; |
| 71 | + var pows = Array(nums.length); |
| 72 | + pows[0] = 1; |
| 73 | + for (var i = 1; i < nums.length; i++) { |
| 74 | + pows[i] = (pows[i - 1] * 2) % mod; |
| 75 | + } |
| 76 | + while (l <= r) { |
| 77 | + if (nums[l] + nums[r] <= target) { |
| 78 | + res += pows[r - l]; |
| 79 | + res %= mod; |
| 80 | + l++; |
| 81 | + } else { |
| 82 | + r--; |
| 83 | + } |
| 84 | + } |
| 85 | + return res; |
| 86 | +}; |
| 87 | +``` |
| 88 | + |
| 89 | +**Explain:** |
| 90 | + |
| 91 | +1. sort the array won't change the result, so we sort it first |
| 92 | +2. then use two pointer to find out answers |
| 93 | +3. keep it in mind: do not let numbers overflow |
| 94 | + |
| 95 | +**Complexity:** |
| 96 | + |
| 97 | +* Time complexity : O(nlog(n)). |
| 98 | +* Space complexity : O(n). |
0 commit comments