We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a358d8f commit 169468eCopy full SHA for 169468e
solution/1400-1499/1498.Number of Subsequences That Satisfy the Given Sum Condition/Solution.cs
@@ -0,0 +1,27 @@
1
+public class Solution {
2
+ public int NumSubseq(int[] nums, int target) {
3
+ const int MOD = 1000000007;
4
+
5
+ Array.Sort(nums);
6
+ int n = nums.Length;
7
+ int leftIndex = 0, rightIndex = n - 1;
8
+ int result = 0;
9
10
+ long[] pow2 = new long[n];
11
+ pow2[0] = 1;
12
+ for (int i = 1; i < n; i++) {
13
+ pow2[i] = (pow2[i - 1] * 2) % MOD;
14
+ }
15
16
+ while (leftIndex <= rightIndex) {
17
+ if (nums[leftIndex] + nums[rightIndex] <= target) {
18
+ result = (int)((result + pow2[rightIndex - leftIndex]) % MOD);
19
+ leftIndex++;
20
+ } else {
21
+ rightIndex--;
22
23
24
25
+ return result;
26
27
+}
0 commit comments