Skip to content
This repository has been archived by the owner on Jun 28, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1057 from osama-swe/main
Browse files Browse the repository at this point in the history
Added my sol to 6 May
  • Loading branch information
7oSkaaa committed May 6, 2023
2 parents 7fd0163 + 23475cd commit 8911a79
Showing 1 changed file with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Author: Osama Ayman
// Time: O(nlogn)
// Space: O(n)
class Solution {

public int numSubseq(int[] nums, int target) {
int mod = (int) 1e9+7;
// Sort the array and apply the 2-pointers strategy
Arrays.sort(nums);
int n = nums.length;
// Precompute the value of 2 to the power of each value.
int[] power = new int[n];
power[0] = 1;
for (int i = 1; i < n; ++i) {
power[i] = (power[i - 1] * 2) % mod;
}

int left = 0;
int right = nums.length-1;
int res = 0;
while(left <= right){
if(nums[left] + nums[right] <= target){
// the number of subsequences is 2 ^ (right-left)
res += power[right - left];
res %= mod;
left++;
}
else right--;
}
return res;
}
}

0 comments on commit 8911a79

Please sign in to comment.