Skip to content

Commit 169468e

Browse files
committed
add c# solution using two-pointer
1 parent a358d8f commit 169468e

File tree

1 file changed

+27
-0
lines changed
  • solution/1400-1499/1498.Number of Subsequences That Satisfy the Given Sum Condition

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)