Skip to content

Commit

Permalink
Create 1498-number-of-subsequences-that-satisfy-the-given-sum-conditi…
Browse files Browse the repository at this point in the history
…on.kt
  • Loading branch information
a93a authored May 22, 2023
1 parent 975ac00 commit 1b990c8
Showing 1 changed file with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
fun numSubseq(nums: IntArray, target: Int): Int {
nums.sort()
val mod = 1000000000 + 7

val pow = IntArray(nums.size)
pow[0] = 1
for (i in 0 until nums.size - 1)
pow[i + 1] = pow[i] * 2 % mod

var left = 0
var right = nums.lastIndex
var res = 0
while (left <= right) {
if (nums[left] + nums[right] > target) {
right--
} else {
res = (res + pow[right - left]) % mod
left++
}
}

return res
}
}

0 comments on commit 1b990c8

Please sign in to comment.