Skip to content
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

Merged
merged 23 commits into from
Oct 12, 2023
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
c08e138
added power_of_4
siddwarr Oct 2, 2023
60e1a83
deleted power_of_4
siddwarr Oct 2, 2023
1720083
Merge branch 'TheAlgorithms:master' into master
siddwarr Oct 3, 2023
595ad2d
Merge branch 'TheAlgorithms:master' into master
siddwarr Oct 3, 2023
2290ff9
added pairs_with_given_sum
siddwarr Oct 3, 2023
1f94b83
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 3, 2023
4251cf5
updated the comment
siddwarr Oct 3, 2023
a8cb552
Merge branch 'count_pairs_with_given_sum' of https://github.com/siddw…
siddwarr Oct 3, 2023
a3ee9eb
updated return hint
siddwarr Oct 3, 2023
f6f790f
updated type hints
siddwarr Oct 3, 2023
6a0daf4
updated the variable
siddwarr Oct 3, 2023
77b6a7c
updated annotation
siddwarr Oct 3, 2023
8bf754d
updated code
siddwarr Oct 3, 2023
42d2c13
updated code
siddwarr Oct 3, 2023
f39e168
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 3, 2023
9ea6c3e
Merge branch 'TheAlgorithms:master' into count_pairs_with_given_sum
siddwarr Oct 11, 2023
fdfe4f4
added the problem link and used defaultdict
siddwarr Oct 12, 2023
a42c88b
Merge branch 'TheAlgorithms:master' into count_pairs_with_given_sum
siddwarr Oct 12, 2023
0752ae1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 12, 2023
d40eaa1
corrected import formatting
siddwarr Oct 12, 2023
ce68b14
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 12, 2023
27dc6d2
Update pairs_with_given_sum.py
cclauss Oct 12, 2023
e4befa3
Update data_structures/arrays/pairs_with_given_sum.py
cclauss Oct 12, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions data_structures/arrays/pairs_with_given_sum.py
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 len([1 for a, b in combinations(arr, 2) if a + b == req_sum])


if __name__ == "__main__":
from doctest import testmod

testmod()