Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //first approach is brute force
- class Solution:
- def countPairs(self, nums: List[int], target: int) -> int:
- arr=sorted(nums)
- i,j=0,len(arr)-1
- ans=0
- while i<=j:
- if arr[i]+arr[j]>=target:
- j-=1
- elif arr[i]+arr[j]<target: #arr[i] can form valid pairs with every element from i+1 to j
- ans+= (j-i)
- i+=1
- return ans
Advertisement
Add Comment
Please, Sign In to add comment