-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
...sy/Count Pairs whose sum is less than target/count-pairs-whose-sum-is-less-than-target.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#{ | ||
# Driver Code Starts | ||
#Initial Template for Python 3 | ||
|
||
import math | ||
|
||
|
||
# } Driver Code Ends | ||
#User function Template for python3 | ||
class Solution: | ||
def countPairs(self, arr, target): | ||
arr.sort() | ||
count = 0 | ||
left, right = 0, len(arr) - 1 | ||
|
||
while left < right: | ||
if arr[left] + arr[right] < target: | ||
count += right - left | ||
left += 1 | ||
else: | ||
right -= 1 | ||
|
||
return count | ||
|
||
|
||
#{ | ||
# Driver Code Starts. | ||
|
||
def main(): | ||
T = int(input()) | ||
while (T > 0): | ||
|
||
A = [int(x) for x in input().strip().split()] | ||
|
||
k = int(input()) | ||
ob = Solution() | ||
print(ob.countPairs(A, k)) | ||
print('~') | ||
T -= 1 | ||
|
||
|
||
if __name__ == "__main__": | ||
main() | ||
|
||
# } Driver Code Ends |