-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1498.cpp
More file actions
27 lines (26 loc) · 752 Bytes
/
1498.cpp
File metadata and controls
27 lines (26 loc) · 752 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
public:
int numSubseq(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());
int n = nums.size();
int mod = 1e9 + 7;
int left = 0;
int right = n - 1;
int count = 0;
vector<int> powerOfTwo(n, 0);
powerOfTwo[0] = 1;
for (int i = 1; i < n; ++i) {
int current = powerOfTwo[i - 1] * 2;
current %= mod;
powerOfTwo[i] = current;
}
while (left <= right) {
while (right >= 0 && nums[right] + nums[left] > target) right--;
if (left > right) break;
count += powerOfTwo[right - left];
count %= mod;
left++;
}
return count;
}
};