Sponsored
Sponsored
This approach involves sorting the array and then using a two pointers method to determine valid subsequences. We focus on the potential minimum and maximum values in a subsequence.
After sorting, use two pointers, one starting from the beginning of the array (for the minimum) and the other from the end (for the maximum). For each minimum, the number of valid subsequences is determined by how many elements from the minimum can pair with elements from the maximum such that their sum is less than or equal to the target. The number of subsequences can be calculated using the binary exponentiation of 2 with the distance between the two pointers.
Time Complexity: O(n log n), due to sorting the array.
Space Complexity: O(1), or O(n) if considering the space used by sorting.
1using System;
2
3public class Solution {
4 private const int MOD = 1000000007;
5
6 private long Power(int x, int y, int p) {
7 long result = 1;
8 long baseValue = x % p;
9 while (y > 0) {
10 if ((y & 1) == 1) {
11 result = (result * baseValue) % p;
12 }
13 baseValue = (baseValue * baseValue) % p;
14 y >>= 1;
}
return result;
}
public int NumSubseq(int[] nums, int target) {
Array.Sort(nums);
long count = 0;
int left = 0, right = nums.Length - 1;
while (left <= right) {
if (nums[left] + nums[right] <= target) {
count = (count + Power(2, right - left, MOD)) % MOD;
left++;
} else {
right--;
}
}
return (int)count;
}
public static void Main(string[] args) {
var solution = new Solution();
int[] nums = {3, 5, 6, 7};
int target = 9;
Console.WriteLine(solution.NumSubseq(nums, target));
}
}
C#'s solution defines a class and methods similar to Python, Java, and C++. The Power
method conducts modulus power calculations efficiently through iterative squaring. Sorting the array lets us find correct subsequences based on minimum and maximum values efficiently.
This approach involves dynamic programming where pre-computed powers of 2 are used to calculate valid subsequences. Using arrays to store results dynamically avoids repetitive calculations and exploits binary indexing for faster computation on potential subsets.
The idea is to create a precompute power array of size n to store power values of 2 up to n. Iterating with this precompute allows identifying valid subsequences without recalculating powers each iteration, enhancing efficiency especially for extremely large arrays.
Time Complexity: O(n log n) for sorting. The DP preparation step is O(n).
Space Complexity: O(n) due to the power array creation.
1from typing import List
2
3MOD = 10**9 + 7
4
5class Solution:
6 def numSubseq(self, nums: List[int], target: int) -> int:
7 nums.sort()
8 n = len(nums)
9 power_2 = [1] * n
10 for i in range(1, n):
11 power_2[i] = power_2[i - 1] * 2 % MOD
12
13 result = 0
14 left, right = 0, n - 1
15
16 while left <= right:
17 if nums[left] + nums[right] <= target:
18 result = (result + power_2[right - left]) % MOD
19 left += 1
20 else:
21 right -= 1
22
23 return result
24
25# Example usage:
26solution = Solution()
27nums = [3, 5, 6, 7]
28target = 9
29print(solution.numSubseq(nums, target))
Python's version of the algorithm sorts the input, establishes precomputed powers, and then evaluates subsequences using these precomputed values instead of recalculating them, optimizing its run within the loop where two pointers iterate through the array, processing validity.