From 55a25f7e538d43ce65cb5928a5c70600c038cb20 Mon Sep 17 00:00:00 2001 From: chayan das Date: Sun, 29 Jun 2025 23:46:43 +0530 Subject: [PATCH] Create 1498. Number of Subsequences That Satisfy the Given Sum Condition1 --- ...nces That Satisfy the Given Sum Condition1 | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 1498. Number of Subsequences That Satisfy the Given Sum Condition1 diff --git a/1498. Number of Subsequences That Satisfy the Given Sum Condition1 b/1498. Number of Subsequences That Satisfy the Given Sum Condition1 new file mode 100644 index 0000000..35eff0f --- /dev/null +++ b/1498. Number of Subsequences That Satisfy the Given Sum Condition1 @@ -0,0 +1,33 @@ +class Solution { +public: + int numSubseq(vector& 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 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; + } +};