“Temukan sepasang elemen dari array yang jumlahnya sama dengan nomor python yang diberikan” Kode Jawaban

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

Temukan sepasang elemen dari array yang jumlahnya sama dengan nomor python yang diberikan

#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]
Intempestive Al Dente

Jawaban yang mirip dengan “Temukan sepasang elemen dari array yang jumlahnya sama dengan nomor python yang diberikan”

Pertanyaan yang mirip dengan “Temukan sepasang elemen dari array yang jumlahnya sama dengan nomor python yang diberikan”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya