Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2bb05ec

Browse files
author
hasibulislam999
committed
Number of Subsequences That Satisfy the Given Sum Condition problem solved
1 parent 610e26f commit 2bb05ec

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Title: Number of Subsequences That Satisfy the Given Sum Condition
3+
* Description: Return the number of non-empty subsequences of nums such that the sum of the minimum and maximum element on it is less or equal to target. Since the answer may be too large, return it modulo 109 + 7.
4+
* Author: Hasibul Islam
5+
* Date: 06/05/2023
6+
*/
7+
8+
/**
9+
* @param {number[]} nums
10+
* @param {number} target
11+
* @return {number}
12+
*/
13+
var numSubseq = function (nums, target) {
14+
const MOD = 1000000007;
15+
16+
nums = nums.sort((a, b) => a - b);
17+
18+
const pows = [1];
19+
20+
for (let i = 1; i < nums.length; i++) {
21+
pows.push((pows[i - 1] * 2) % MOD);
22+
}
23+
24+
let left = 0;
25+
let right = nums.length - 1;
26+
let ans = 0;
27+
28+
while (left <= right) {
29+
if (nums[left] + nums[right] > target) {
30+
right--;
31+
} else {
32+
ans = ans + pows[right - left];
33+
left++;
34+
}
35+
}
36+
37+
return ans % MOD;
38+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /