MCPcopy Create free account
hub / github.com/Vishruth-S/CompetitiveCode / getPairsCount

Function getPairsCount

GeeksForGeeks/SumOfPairs/solution2.py:5–26  ·  view source on GitHub ↗
(arr, n, sum)

Source from the content-addressed store, hash-verified

3# GeeksforGeeks Explanation: https://youtu.be/bvKMZXc0jQU
4
5def getPairsCount(arr, n, sum):
6
7 m = [0] * 1000
8
9 # Store counts of all elements in map m
10 for i in range(0, n):
11 m[arr[i]] # Stores the frequency of the number in the array
12 m[arr[i]] += 1
13
14 twice_count = 0
15
16 # Iterate through each element and increment the count (Every pair is counted twice)
17 for i in range(0, n):
18
19 twice_count += m[sum - arr[i]]
20
21 # if (arr[i], arr[i]) pair satisfies the condition, then we need to ensure that the count is decreased by one such that the (arr[i], arr[i]) pair is not considered
22 if (sum - arr[i] == arr[i]):
23 twice_count -= 1
24
25 # return the half of twice_count
26 return int(twice_count / 2)
27
28n = int(input())
29arr = list(map(int,input().split()))

Callers 1

solution2.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected