MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / pairs_with_sum

Function pairs_with_sum

data_structures/arrays/pairs_with_given_sum.py:13–23  ·  view source on GitHub ↗

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

(arr: list, req_sum: int)

Source from the content-addressed store, hash-verified

11
12
13def pairs_with_sum(arr: list, req_sum: int) -> int:
14 """
15 Return the no. of pairs with sum "sum"
16 >>> pairs_with_sum([1, 5, 7, 1], 6)
17 2
18 >>> pairs_with_sum([1, 1, 1, 1, 1, 1, 1, 1], 2)
19 28
20 >>> pairs_with_sum([1, 7, 6, 2, 5, 4, 3, 1, 9, 8], 7)
21 4
22 """
23 return len([1 for a, b in combinations(arr, 2) if a + b == req_sum])
24
25
26if __name__ == "__main__":

Callers

nothing calls this directly

Calls 1

combinationsFunction · 0.50

Tested by

no test coverage detected