“pares de contagem com dado soma python” Respostas de código

pares de contagem com dado soma 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
Blue Cloud: Weird Coder

Contando pares em uma matriz, resultando em uma determinada soma

<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>
Panicky Parrot

Contando pares em uma matriz, resultando em uma determinada soma

<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>
Panicky Parrot

Respostas semelhantes a “pares de contagem com dado soma python”

Perguntas semelhantes a “pares de contagem com dado soma python”

Mais respostas relacionadas para “pares de contagem com dado soma python” em Python

Procure respostas de código populares por idioma

Procurar outros idiomas de código