Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

count pairs with given sum python

count = 0
given_sum = 7
x = 4
y = 3
pair = x + y
if pair == 7:
	count += 1
#count goes up if the sum of x and y is the given number
Comment

Counting Pairs in an Array, resulting in a given sum

<script>
/* javascript implementation of simple method to find count of
pairs with given sum*/
    var arr = [ 1, 5, 7, -1, 5 ];
 
    // Returns number of pairs in arr[0..n-1] with sum equal
    // to 'sum'
    function getPairsCount(n , sum) {
        var hm = new Map();
 
        // Store counts of all elements in map hm
        for (var i = 0; i < n; i++) {
 
            // initializing value to 0, if key not found
            if (!hm.has(arr[i]))
                hm.set(arr[i], 0);
 
            hm.set(arr[i], hm.get(arr[i]) + 1);
        }
        var twice_count = 0;
 
        // iterate through each element and increment the
        // count (Notice that every pair is counted twice)
        for (i = 0; i < n; i++) {
            if (hm.get(sum - arr[i]) != null)
                twice_count += hm.get(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--;
        }
 
        // return the half of twice_count
        return twice_count / 2;
    }
 
    // Driver method to test the above function
        var sum = 6;
        document.write("Count of pairs is " + getPairsCount(arr.length, sum));
 
// This code is contributed by umadevi9616
</script>
Comment

Counting Pairs in an Array, resulting in a given sum

<script>
// javascript 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'
    function getPairsCount(arr , n , k) {
        var m = new Map();
        var count = 0;
        for (var i = 0; i < n; i++) {
            if (m.has(k - arr[i])) {
                count += m.get(k - arr[i]);
            }
            if (m.has(arr[i])) {
                m.set(arr[i], m.get(arr[i]) + 1);
            } else {
                m.set(arr[i], 1);
            }
        }
        return count;
    }
 
    // Driver function to test the above function
        var arr = [ 1, 5, 7, -1, 5 ];
        var n = arr.length;
        var sum = 6;
        document.write("Count of pairs is " + getPairsCount(arr, n, sum));
 
// This code is contributed by umadevi9616
</script>
Comment

find a pair of elements from an array whose sum equals a given number python

#Given an array of integers and a number, write a function that finds two elements from the array whose sum is equal to the given number.
def find_pair(arr, num):
  for i in range(len(arr)):
    for j in range(i + 1, len(arr)):
      if arr[i] + arr[j] == num:
        return [arr[i], arr[j]]

print(find_pair([3, 6, 8, -8, 10, 8], 16))
[6, 10]
Comment

PREVIOUS NEXT
Code Example
Python :: pyautogui 
Python :: positional only arguments python 
Python :: Yield Expressions in python 
Python :: merge sorting algorithm 
Python :: write hexadecimal in python 
Python :: python file 
Python :: data encapsulation in python 
Python :: python copy vs deepcopy 
Python :: Multiple list comprehension 
Python :: python linkedin api 
Python :: python OSError: [Errno 22] Invalid argument: 
Python :: python declare 2d list 
Python :: python array of objects 
Python :: python eval 
Python :: Python NumPy concatenate Function Syntax 
Python :: tuplein python 
Python :: stack.pop() 
Python :: k-means clustering 
Python :: python variable type 
Python :: queryset django 
Python :: site:*.instagram.com 
Python :: what is an indefinite loop 
Python :: speechapi 
Python :: randint without repitition 
Python :: django snippet 800 
Python :: reverse words and swapcase in python 
Python :: python zeromq timeout 
Python :: pygame download for python 3.10 
Python :: #clearing all keys new key in python 
Python :: Filter xarray (dataarray) 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =