From 3c6068e7ffbcd39a29cd41755c9696bd3f4be288 Mon Sep 17 00:00:00 2001 From: Tidimatso Anthony <167877348+tidianthony@users.noreply.github.com> Date: Tue, 3 Sep 2024 19:02:29 +0200 Subject: [PATCH 1/2] Update pairs_with_given_sum.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Dictionary Usage: We use a dictionary seen to keep track of the number of times each number has appeared so far. For each number num in the array, we calculate its complement (req_sum - num). If this complement has been seen before, then it means there are pairs that sum up to req_sum, and we add the count of such complements to our count. Efficiency: This approach processes each element of the array exactly once and performs operations in constant time for each element. Thus, the time complexity is 𝑂 ( 𝑛 ) O(n), where 𝑛 n is the number of elements in the array. Readability: The code is more straightforward and easier to understand compared to generating combinations. It leverages hash maps for efficient lookups and counting. --- .../arrays/pairs_with_given_sum.py | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/data_structures/arrays/pairs_with_given_sum.py b/data_structures/arrays/pairs_with_given_sum.py index b27bd78e1e0f..9faf483bd331 100644 --- a/data_structures/arrays/pairs_with_given_sum.py +++ b/data_structures/arrays/pairs_with_given_sum.py @@ -7,12 +7,10 @@ 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: +def pairs_with_sum(arr: list[int], req_sum: int) -> int: """ - Return the no. of pairs with sum "sum" + Return the number of pairs with sum equal to req_sum. + >>> pairs_with_sum([1, 5, 7, 1], 6) 2 >>> pairs_with_sum([1, 1, 1, 1, 1, 1, 1, 1], 2) @@ -20,10 +18,22 @@ def pairs_with_sum(arr: list, req_sum: int) -> int: >>> 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]) - + count = 0 + seen = {} + + for num in arr: + complement = req_sum - num + if complement in seen: + count += seen[complement] + + if num in seen: + seen[num] += 1 + else: + seen[num] = 1 + + return count if __name__ == "__main__": from doctest import testmod - testmod() + From 881b4eddd25f63958ff6bf1025085b320541cd06 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 3 Sep 2024 17:04:45 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/pairs_with_given_sum.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/data_structures/arrays/pairs_with_given_sum.py b/data_structures/arrays/pairs_with_given_sum.py index 9faf483bd331..53c2b031f729 100644 --- a/data_structures/arrays/pairs_with_given_sum.py +++ b/data_structures/arrays/pairs_with_given_sum.py @@ -7,10 +7,11 @@ https://practice.geeksforgeeks.org/problems/count-pairs-with-given-sum5022/0 """ + def pairs_with_sum(arr: list[int], req_sum: int) -> int: """ Return the number of pairs with sum equal to req_sum. - + >>> pairs_with_sum([1, 5, 7, 1], 6) 2 >>> pairs_with_sum([1, 1, 1, 1, 1, 1, 1, 1], 2) @@ -20,20 +21,21 @@ def pairs_with_sum(arr: list[int], req_sum: int) -> int: """ count = 0 seen = {} - + for num in arr: complement = req_sum - num if complement in seen: count += seen[complement] - + if num in seen: seen[num] += 1 else: seen[num] = 1 - + return count + if __name__ == "__main__": from doctest import testmod - testmod() + testmod()