Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def maxSumPairWithDifferenceLessThanK(self, arr, n, k):
- '''
- We are sorting in decreasing order because we need to findout
- maxpair sum so we will try to pair maxnumber with 2nd max or 3rd max like that
- '''
- arr.sort(reverse=True)
- i,j=0,1
- ans=0
- while i<n:
- #j is in bound
- if j<n:
- #you have found that pair has sum<k
- if arr[i]-arr[j]<k:
- ans+=arr[i]+arr[j]
- i,j=i+2,j+2
- #you didnt find j than checking for j+1 is waste since they
- # are in decreasing order.So move to i+1
- else:
- i=i+1
- j=i+1
- #j has reached out of bounds so increment i
- else:
- i=i+1
- j=i+1
- return ans
Add Comment
Please, Sign In to add comment