-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Count pairs with given sum #10282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Count pairs with given sum #10282
Changes from 21 commits
c08e138
60e1a83
1720083
595ad2d
2290ff9
1f94b83
4251cf5
a8cb552
a3ee9eb
f6f790f
6a0daf4
77b6a7c
8bf754d
42d2c13
f39e168
9ea6c3e
fdfe4f4
a42c88b
0752ae1
d40eaa1
ce68b14
27dc6d2
e4befa3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
""" | ||
Author : Siddharth Warrier | ||
Date : October 3, 2023 | ||
|
||
Task: | ||
Count the no of pairs in a given array with given sum | ||
Problem URL- https://practice.geeksforgeeks.org/problems/count-pairs-with-given-sum5022/0 | ||
|
||
Implementation notes: Using hashing | ||
The idea is that we hash the array in a dictionary | ||
Then go through the elements of the array | ||
We subtract this with the given sum | ||
and check if that is there in the array | ||
We also check the edge cases like if there are multiple same elements | ||
Finally we divide the count by 2 | ||
to avoid the same pair getting counted twice | ||
""" | ||
|
||
|
||
def pairs_with_sum(arr: list, req_sum: int) -> int: | ||
""" | ||
Return the no. of pairs with sum "sum" | ||
|
||
>>> pairs_with_sum([1,5,7,1],6) | ||
2 | ||
>>> pairs_with_sum([1,1,1,1,1,1,1,1],2) | ||
28 | ||
>>> pairs_with_sum([1,7,6,2,5,4,3,1,9,8],7) | ||
4 | ||
""" | ||
d = defaultdict(int) | ||
for i in arr: | ||
d[i] += 1 | ||
ans = 0 | ||
for i in arr: | ||
d[i] -= 1 | ||
if req_sum - i in d and d[req_sum - i] != 0: | ||
ans += d[req_sum - i] - 1 | ||
d[i] += 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are subtracting one, and then adding one after an irrelevant if statement. Why? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So the reason I included that is because let's say the given number is 8 and you have 4 in your array. Now if u don't decrement d[4] by 1 then from the code it will check if 4 exists in the dictionary and as it does ans will be incremented. So even though there is only one 4 in the array it will still be considered. I could remove that and put if d[req-i]!=1 |
||
return ans // 2 | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
from collections import defaultdict | ||
|
||
doctest.testmod() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.