“Hitung pasangan dengan jumlah ular python yang diberikan” Kode Jawaban

Hitung pasangan dengan jumlah ular python yang diberikan

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

Menghitung pasangan dalam array, menghasilkan jumlah yang diberikan

<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

Menghitung pasangan dalam array, menghasilkan jumlah yang diberikan

<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

Jawaban yang mirip dengan “Hitung pasangan dengan jumlah ular python yang diberikan”

Pertanyaan yang mirip dengan “Hitung pasangan dengan jumlah ular python yang diberikan”

Lebih banyak jawaban terkait untuk “Hitung pasangan dengan jumlah ular python yang diberikan” di Python

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya