Skip to content

Commit 66efd0d

Browse files
authored
Merge pull request #3350 from Aksshay88/main
Solution for 1498-Number of Subsequences That Satisfy the Given Sum …
2 parents a6381a4 + 8cf400c commit 66efd0d

File tree

1 file changed

+27
-0
lines changed

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+
class Solution {
2+
public int numSubseq(int[] nums, int target) {
3+
int mod = 1000000007;
4+
int n = nums.length;
5+
Arrays.sort(nums);
6+
7+
int[] pow2 = new int[n];
8+
pow2[0] = 1;
9+
for (int i = 1; i < n; i++) {
10+
pow2[i] = (pow2[i - 1] * 2) % mod;
11+
}
12+
13+
int left = 0, right = n - 1;
14+
int count = 0;
15+
16+
while (left <= right) {
17+
if (nums[left] + nums[right] <= target) {
18+
count = (count + pow2[right - left]) % mod;
19+
left++;
20+
} else {
21+
right--;
22+
}
23+
}
24+
25+
return count;
26+
}
27+
}

0 commit comments

Comments
 (0)