-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
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
Merged
cclauss
merged 23 commits into
TheAlgorithms:master
from
siddwarr:count_pairs_with_given_sum
Oct 12, 2023
Merged
Count pairs with given sum #10282
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
c08e138
added power_of_4
siddwarr 60e1a83
deleted power_of_4
siddwarr 1720083
Merge branch 'TheAlgorithms:master' into master
siddwarr 595ad2d
Merge branch 'TheAlgorithms:master' into master
siddwarr 2290ff9
added pairs_with_given_sum
siddwarr 1f94b83
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4251cf5
updated the comment
siddwarr a8cb552
Merge branch 'count_pairs_with_given_sum' of https://github.com/siddw…
siddwarr a3ee9eb
updated return hint
siddwarr f6f790f
updated type hints
siddwarr 6a0daf4
updated the variable
siddwarr 77b6a7c
updated annotation
siddwarr 8bf754d
updated code
siddwarr 42d2c13
updated code
siddwarr f39e168
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9ea6c3e
Merge branch 'TheAlgorithms:master' into count_pairs_with_given_sum
siddwarr fdfe4f4
added the problem link and used defaultdict
siddwarr a42c88b
Merge branch 'TheAlgorithms:master' into count_pairs_with_given_sum
siddwarr 0752ae1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d40eaa1
corrected import formatting
siddwarr ce68b14
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 27dc6d2
Update pairs_with_given_sum.py
cclauss e4befa3
Update data_structures/arrays/pairs_with_given_sum.py
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,28 @@ | ||
#!/usr/bin/env python3 | ||
|
||
""" | ||
Given an array of integers and an integer req_sum, find the number of pairs of array | ||
elements whose sum is equal to req_sum. | ||
|
||
https://practice.geeksforgeeks.org/problems/count-pairs-with-given-sum5022/0 | ||
""" | ||
from itertools import combinations | ||
|
||
|
||
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 | ||
""" | ||
return sum(int(bool(a + b == req_sum)) for a, b in combinations(arr, 2)) | ||
|
||
|
||
if __name__ == "__main__": | ||
from doctest import testmod | ||
|
||
testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.