Number of Subsequences That Satisfy the Given Sum Condition - Complete Solution Guide
Number of Subsequences That Satisfy the Given Sum Condition is LeetCode problem 1498, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 10 9 + 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 <= 9) [3,5] -> (3 + 5 <= 9) [3,5,6] -> (3 + 6 <= 9) [3,6] -> (3
Detailed Explanation
The problem asks us to find the number of non-empty subsequences of a given array `nums` whose minimum and maximum element sum up to a value less than or equal to a given `target`. The result needs to be calculated modulo 10^9 + 7. A subsequence is a sequence that can be derived from an array by deleting some or no elements without changing the order of the remaining elements. For example, given `nums = [3, 5, 6, 7]` and `target = 9`, we want to find subsequences like `[3]`, `[3, 5]`, `[3, 6]` and `[3, 5, 6]` because the sum of their minimum and maximum values is at most 9.
Solution Approach
The solution first sorts the input array `nums`. Then, it uses a two-pointer approach with `left` and `right` pointers initialized to the beginning and the end of the sorted array, respectively. For each element at the `left` pointer (potential minimum value), it moves the `right` pointer to the largest element such that `nums[left] + nums[right] <= target`. The number of subsequences that can be formed using the elements between `left` and `right` is 2^(right - left). These counts are accumulated (modulo 10^9 + 7) to produce the final result. Powers of 2 are pre-calculated to optimize calculations within the main loop.
Step-by-Step Algorithm
- Step 1: Sort the input array `nums` in ascending order.
- Step 2: Initialize `left` to 0 and `right` to `n - 1`, where `n` is the length of `nums`.
- Step 3: Precalculate powers of 2 modulo 10^9 + 7 and store them in an array `powers` (powers[i] = 2^i % mod).
- Step 4: Initialize `ans` to 0, which will store the count of valid subsequences.
- Step 5: While `left <= right`:
- Step 6: If `nums[left] + nums[right] <= target`, it means all elements between `left` and `right` can be part of a valid subsequence with `nums[left]` as the minimum. Add `powers[right - left]` to `ans` modulo 10^9 + 7. Increment `left`.
- Step 7: Else, `nums[left] + nums[right] > target`, which means `nums[right]` is too large to form a valid subsequence with `nums[left]` as the minimum. Decrement `right`.
- Step 8: Return `ans`.
Key Insights
- Insight 1: Sorting the input array `nums` allows us to efficiently find the minimum and maximum elements by considering elements from the beginning and the end of the sorted array.
- Insight 2: For a given minimum element `nums[left]`, the number of subsequences that can be formed with elements between `nums[left]` and the maximum element `nums[right]` (such that `nums[left] + nums[right] <= target`) is 2^(right - left). This is because each element in between can either be included or excluded in the subsequence.
- Insight 3: Precalculating powers of 2 modulo 10^9 + 7 avoids repeated calculations and ensures the result doesn't overflow.
Complexity Analysis
Time Complexity: O(n log n)
Space Complexity: O(n)
Topics
This problem involves: Array, Two Pointers, Binary Search, Sorting.
Companies
Asked at: Turing.