Python Program for Count pairs with given sum
Last Updated :
15 Feb, 2023
Given an array of integers, and a number 'sum', find the number of pairs of integers in the array whose sum is equal to 'sum'.
Examples:
Input : arr[] = {1, 5, 7, -1},
sum = 6
Output : 2
Pairs with sum 6 are (1, 5) and (7, -1)
Input : arr[] = {1, 5, 7, -1, 5},
sum = 6
Output : 3
Pairs with sum 6 are (1, 5), (7, -1) &
(1, 5)
Input : arr[] = {1, 1, 1, 1},
sum = 2
Output : 6
There are 3! pairs with sum 2.
Input : arr[] = {10, 12, 10, 15, -1, 7, 6,
5, 4, 2, 1, 1, 1},
sum = 11
Output : 9
Expected time complexity O(n)
Naive Solution - A simple solution is to traverse each element and check if there's another number in the array which can be added to it to give sum.
Python3
# Python3 implementation of simple method
# to find count of pairs with given sum.
# Returns number of pairs in arr[0..n-1]
# with sum equal to 'sum'
def getPairsCount(arr, n, sum):
count = 0 # Initialize result
# Consider all possible pairs
# and check their sums
for i in range(0, n):
for j in range(i + 1, n):
if arr[i] + arr[j] == sum:
count += 1
return count
# Driver function
arr = [1, 5, 7, -1, 5]
n = len(arr)
sum = 6
print("Count of pairs is",
getPairsCount(arr, n, sum))
# This code is contributed by Smitha Dinesh Semwal
OutputCount of pairs is 3
Time Complexity: O(n2)
Auxiliary Space: O(1)
Efficient solution -
A better solution is possible in O(n) time. Below is the Algorithm -
- Create a map to store frequency of each number in the array. (Single traversal is required)
- In the next traversal, for every element check if it can be combined with any other element (other than itself!) to give the desired sum. Increment the counter accordingly.
- After completion of second traversal, we'd have twice the required value stored in counter because every pair is counted two times. Hence divide count by 2 and return.
Below is the implementation of above idea :
Python3
# Python 3 implementation of simple method
# to find count of pairs with given sum.
import sys
# Returns number of pairs in arr[0..n-1]
# with sum equal to 'sum'
def getPairsCount(arr, n, sum):
m = [0] * 1000
# Store counts of all elements in map m
for i in range(0, n):
m[arr[i]] += 1
twice_count = 0
# Iterate through each element and increment
# the count (Notice that every pair is
# counted twice)
for i in range(0, n):
twice_count += m[sum - arr[i]]
# 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
if (sum - arr[i] == arr[i]):
twice_count -= 1
# return the half of twice_count
return int(twice_count / 2)
# Driver function
arr = [1, 5, 7, -1, 5]
n = len(arr)
sum = 6
print("Count of pairs is", getPairsCount(arr,
n, sum))
# This code is contributed by
# Smitha Dinesh Semwal
OutputCount of pairs is 3
Time Complexity: O(n)
Auxiliary Space: O(n)
The extra space is used to store the elements in the map.
Please refer complete article on Count pairs with given sum for more details!
Similar Reads
2 Sum - Count pairs with given sum Given an array arr[] of n integers and a target value, the task is to find the number of pairs of integers in the array whose sum is equal to target.Examples: Input: arr[] = {1, 5, 7, -1, 5}, target = 6Output: 3Explanation: Pairs with sum 6 are (1, 5), (7, -1) & (1, 5). Input: arr[] = {1, 1, 1,
9 min read
Javascript Program to Count pairs with given sum Given an array of integers, and a number 'sum', find the number of pairs of integers in the array whose sum is equal to 'sum'.Note: Duplicate pairs are also allowed. Examples: Input : arr[] = {1, 5, 7, -1}, sum = 6 Output : 2 Pairs with sum 6 are (1, 5) and (7, -1) Input : arr[] = {1, 5, 7, -1, 5},
2 min read
Python Program to Find Sum of Array Given an array of integers, find the sum of its elements. Examples: Input : arr[] = {1, 2, 3}Output : 6Explanation: 1 + 2 + 3 = 6This Python program calculates the sum of an array by iterating through each element and adding it to a running total. The sum is then returned. An example usage is provid
4 min read
Python List Comprehension to find pair with given sum from two arrays Given two unsorted arrays of distinct elements, the task is to find all pairs from both arrays whose sum is equal to x. Examples: Input : arr1 = [-1, -2, 4, -6, 5, 7] arr2 = [6, 3, 4, 0] x = 8 Output : [(5, 3), (4, 4)] Input : arr1 = [1, 2, 4, 5, 7] arr2 = [5, 6, 3, 4, 8] x = 9 Output : [(1, 8), (4,
2 min read
How to Perform a COUNTIF Function in Python? In this article, we will discuss how to perform a COUNTIF function in Python. COUNTIF We use this function to count the elements if the condition is satisfied. Notice that the word stands as COUNT + IF. That means we want to count the element if the condition that is provided is satisfied. Approach
4 min read
Python - Numbers in a list within a given range We are given a list and we are given a range we need to count how many number lies in the given range. For example, we are having a list n = [5, 15, 25, 35, 45, 55, 65, 75] and we are having range lower=20, upper=60 so we need to count how many element lies between this range so that the output shou
4 min read