Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions 1498. Number of Subsequences That Satisfy the Given Sum Condition1
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Solution {
public:
int numSubseq(vector<int>& nums, int target) {
int mod = 1e9 + 7;
long long ans = 0;
sort(nums.begin(), nums.end());
int n = nums.size() - 1;

// Precompute powers of 2 modulo mod
vector<int> powers(nums.size(), 1);
for (int i = 1; i < nums.size(); ++i) {
powers[i] = (powers[i - 1] * 2) % mod;
}

for (int i = 0; i <= n; i++) {
int start = i, end = n, possible = -1;
while (start <= end) {
int mid = start + (end - start) / 2;
if (nums[i] + nums[mid] <= target) {
possible = mid;
start = mid + 1;
} else {
end = mid - 1;
}
}

if (possible == -1) continue;
ans = (ans + powers[possible - i]) % mod;
}

return ans;
}
};
Loading