Number of Subsequences That Satisfy the Given Sum Condition LeetCode solution in Java
1class Solution {2public int numSubseq(int[] nums, int target) {3int n = nums.length;4int i = 0, j = n - 1;5int mod = (int) 1e9 + 7;6Arrays.sort(nums);7int[] pow = new int[n];8pow[0] = 1;9int count = 0;10for (int z = 1; z < n; z++) {11pow[z] = (pow[z - 1] * 2) % mod;12}1314while (i <= j) {15if ((nums[i] + nums[j]) <= target) {16count = (count + pow[j - i]) % mod;17i++;18} else if ((nums[i] + nums[j]) > target) j--;19}20return count;21}22}