Skip to content

Commit 1b990c8

Browse files
authored
Create 1498-number-of-subsequences-that-satisfy-the-given-sum-condition.kt
1 parent 975ac00 commit 1b990c8

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
fun numSubseq(nums: IntArray, target: Int): Int {
3+
nums.sort()
4+
val mod = 1000000000 + 7
5+
6+
val pow = IntArray(nums.size)
7+
pow[0] = 1
8+
for (i in 0 until nums.size - 1)
9+
pow[i + 1] = pow[i] * 2 % mod
10+
11+
var left = 0
12+
var right = nums.lastIndex
13+
var res = 0
14+
while (left <= right) {
15+
if (nums[left] + nums[right] > target) {
16+
right--
17+
} else {
18+
res = (res + pow[right - left]) % mod
19+
left++
20+
}
21+
}
22+
23+
return res
24+
}
25+
}

0 commit comments

Comments
 (0)