1498. Number of Subsequences That Satisfy the Given Sum Condition LeetCode Solution

In this guide, you will get 1498. Number of Subsequences That Satisfy the Given Sum Condition LeetCode Solution with the best time and space complexity. The solution to Number of Subsequences That Satisfy the Given Sum Condition problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Number of Subsequences That Satisfy the Given Sum Condition solution in C++
  4. Number of Subsequences That Satisfy the Given Sum Condition solution in Java
  5. Number of Subsequences That Satisfy the Given Sum Condition solution in Python
  6. Additional Resources
1498. Number of Subsequences That Satisfy the Given Sum Condition LeetCode Solution image

Problem Statement of Number of Subsequences That Satisfy the Given Sum Condition

You are given an array of integers nums and an integer target.
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.

Example 1:

Input: nums = [3,5,6,7], target = 9
Output: 4
Explanation: There are 4 subsequences that satisfy the condition.
[3] -> Min value + max value <= target (3 + 3 (3 + 5 (3 + 6 (3 + 6 <= 9)

Example 2:

Input: nums = [3,3,6,8], target = 10
Output: 6
Explanation: There are 6 subsequences that satisfy the condition. (nums can have repeated numbers).
[3] , [3] , [3,3], [3,6] , [3,6] , [3,3,6]

Example 3:

Input: nums = [2,3,3,4,6,7], target = 12
Output: 61
Explanation: There are 63 non-empty subsequences, two of them do not satisfy the condition ([6,7], [7]).
Number of valid subsequences (63 – 2 = 61).

See also  129. Sum Root to Leaf Numbers LeetCode Solution

Constraints:

1 <= nums.length <= 105
1 <= nums[i] <= 106
1 <= target <= 106

Complexity Analysis

  • Time Complexity: O(\texttt{sort})
  • Space Complexity: O(n)

1498. Number of Subsequences That Satisfy the Given Sum Condition LeetCode Solution in C++

class Solution {
 public:
  int numSubseq(vector<int>& nums, int target) {
    constexpr int kMod = 1'000'000'007;
    const int n = nums.size();
    int ans = 0;
    vector<int> pows(n, 1);  // pows[i] = 2^i % kMod

    for (int i = 1; i < n; ++i)
      pows[i] = pows[i - 1] * 2 % kMod;

    ranges::sort(nums);

    for (int l = 0, r = n - 1; l <= r;)
      if (nums[l] + nums[r] <= target) {
        ans += pows[r - l];
        ans %= kMod;
        ++l;
      } else {
        --r;
      }

    return ans;
  }
};
/* code provided by PROGIEZ */

1498. Number of Subsequences That Satisfy the Given Sum Condition LeetCode Solution in Java

class Solution {
  public int numSubseq(int[] nums, int target) {
    final int kMod = 1_000_000_007;
    final int n = nums.length;
    int ans = 0;
    int[] pows = new int[n]; // pows[i] = 2^i % kMod
    pows[0] = 1;

    for (int i = 1; i < n; ++i)
      pows[i] = pows[i - 1] * 2 % kMod;

    Arrays.sort(nums);

    for (int l = 0, r = n - 1; l <= r;)
      if (nums[l] + nums[r] <= target) {
        ans += pows[r - l];
        ans %= kMod;
        ++l;
      } else {
        --r;
      }

    return ans;
  }
}
// code provided by PROGIEZ

1498. Number of Subsequences That Satisfy the Given Sum Condition LeetCode Solution in Python

class Solution:
  def numSubseq(self, nums: list[int], target: int) -> int:
    kMod = 1_000_000_007
    n = len(nums)
    ans = 0

    nums.sort()

    l = 0
    r = n - 1
    while l <= r:
      if nums[l] + nums[r] <= target:
        ans += pow(2, r - l, kMod)
        l += 1
      else:
        r -= 1

    return ans % kMod
# code by PROGIEZ

Additional Resources

See also  8. String to Integer (atoi) LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.