mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Merge branch 'count_pairs_with_given_sum' of https://github.com/siddwarr/Python_hacktoberfest into count_pairs_with_given_sum
This commit is contained in:
commit
a8cb5526d0
@ -30,16 +30,16 @@ def pairs_with_sum(arr, k):
|
||||
d = {}
|
||||
for i in arr:
|
||||
if i in d:
|
||||
d[i]+=1
|
||||
d[i] += 1
|
||||
else:
|
||||
d[i] = 1
|
||||
ans = 0
|
||||
for i in arr:
|
||||
d[i]-=1
|
||||
if k-i in d and d[k-i]!=0:
|
||||
ans+=d[k-i]
|
||||
d[i]+=1
|
||||
return ans//2
|
||||
d[i] -= 1
|
||||
if k - i in d and d[k - i] != 0:
|
||||
ans += d[k - i]
|
||||
d[i] += 1
|
||||
return ans // 2
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
Loading…
x
Reference in New Issue
Block a user