python3
class Solution: def purchasePlans(self, nums: List[int], target: int) -> int: mod = 1000000007 res = 0 nums = [i for i in nums if i < target] n = len(nums) - 1 m = 0 nums.sort() while(m < n): if nums[n] + nums[m] <= target: res += n - m m += 1 if nums[n] + nums[m] > target: n -= 1 return res % mod
java
class Solution { public int purchasePlans(int[] nums, int target) { int res = 0, s = 0, len = nums.length - 1; Arrays.sort(nums); while (s < len) { while(len >= 0 && (nums[s] + nums[len]) > target) len--; if(s < len) res += (len - s); res %= 1000000007; s++ ; } return res; } }