| 3 | # GeeksforGeeks Explanation: https://youtu.be/bvKMZXc0jQU |
| 4 | |
| 5 | def 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 | |
| 28 | n = int(input()) |
| 29 | arr = list(map(int,input().split())) |