Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

find pair with given sum in the array

#include <iostream>
#include <unordered_map>
using namespace std;
 
// Function to find a pair in an array with a given sum using hashing
void findPair(int nums[], int n, int target)
{
    // create an empty map
    unordered_map<int, int> map;
 
    // do for each element
    for (int i = 0; i < n; i++)
    {
        // check if pair (nums[i], target - nums[i]) exists
 
        // if the difference is seen before, print the pair
        if (map.find(target - nums[i]) != map.end())
        {
            cout << "Pair found (" << nums[map[target - nums[i]]] << ", "
                 << nums[i] << ")
";
            return;
        }
 
        // store index of the current element in the map
        map[nums[i]] = i;
    }
 
    // we reach here if the pair is not found
    cout << "Pair not found";
}
 
int main()
{
    int nums[] = { 8, 7, 2, 5, 3, 1 };
    int target = 10;
 
    int n = sizeof(nums)/sizeof(nums[0]);
 
    findPair(nums, n, target);
 
    return 0;
}
Comment

find pair with given sum in the array

#include <iostream>
#include <algorithm>
using namespace std;
 
// Function to find a pair in an array with a given sum using sorting
void findPair(int nums[], int n, int target)
{
    // sort the array in ascending order
    sort(nums, nums + n);
 
    // maintain two indices pointing to endpoints of the array
    int low = 0;
    int high = n - 1;
 
    // reduce the search space `nums[low…high]` at each iteration of the loop
 
    // loop till the search space is exhausted
    while (low < high)
    {
        // sum found
        if (nums[low] + nums[high] == target)
        {
            cout << "Pair found (" << nums[low] << ", " << nums[high] << ")
";
            return;
        }
 
        // increment `low` index if the total is less than the desired sum;
        // decrement `high` index if the total is more than the desired sum
        (nums[low] + nums[high] < target)? low++: high--;
    }
 
    // we reach here if the pair is not found
    cout << "Pair not found";
}
 
int main()
{
    int nums[] = { 8, 7, 2, 5, 3, 1 };
    int target = 10;
 
    int n = sizeof(nums)/sizeof(nums[0]);
 
    findPair(nums, n, target);
 
    return 0;
}
Comment

find pair with given sum in the array

#include <stdio.h>

void findPair(int nums[], int n, int target)
{
    // consider each element except the last
    for (int i = 0; i < n - 1; i++)
    {
        // start from the i'th element until the last element
        for (int j = i + 1; j < n; j++)
        {
            // if the desired sum is found, print it
            if (nums[i] + nums[j] == target)
            {
                printf("Pair found (%d, %d)
", nums[i], nums[j]);
                return;
            }
        }
    }
    // we reach here if the pair is not found
    printf("Pair not found");
}
 
int main(void)
{
    int nums[] = { 8, 7, 2, 5, 3, 1 };
    int target = 10;
 
    int n = sizeof(nums)/sizeof(nums[0]);
 
    findPair(nums, n, target);
 
    return 0;
}
Comment

Find A Pair With The Given Sum In An Array

class Main
{
    // Naive method to find a pair in an array with a given sum
    public static void findPair(int[] nums, int target)
    {
        // consider each element except the last
        for (int i = 0; i < nums.length - 1; i++)
        {
            // start from the i'th element until the last element
            for (int j = i + 1; j < nums.length; j++)
            {
                // if the desired sum is found, print it
                if (nums[i] + nums[j] == target)
                {
                    System.out.println("Pair found (" + nums[i] + "," + nums[j] + ")");
                    return;
                }
            }
        }
 
        // we reach here if the pair is not found
        System.out.println("Pair not found");
    }
 
    public static void main (String[] args)
    {
        int[] nums = { 8, 7, 2, 5, 3, 1 };
        int target = 10;
 
        findPair(nums, target);
    }
}
Comment

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
Javascript :: axios async await 
Javascript :: dynamic key in javascript object 
Javascript :: /function 
Javascript :: what is javascript 
Javascript :: javascript if else 
Javascript :: javascript function with array parameter 
Javascript :: react router refreshes page 
Javascript :: js timer 
Javascript :: excel json to table 
Javascript :: get x y z position of mouse javascript 
Javascript :: web app let user confirm closing tab 
Javascript :: insert property in json file with bash 
Javascript :: js multibyte string length 
Javascript :: javascript sort strings alphabetically 
Javascript :: frame-grab js 
Javascript :: JAVASCRIPT FILTRER TABLEAU MULTIDIMENSIONNEL 
Javascript :: document.getelementbyid( timeend ).value example 
Javascript :: immutablejs update use 
Javascript :: object for loop 
Javascript :: document.elementFromPoint 
Javascript :: javascript remove last charcter from string 
Javascript :: quagga node 
Javascript :: add class to random element 
Javascript :: npm can i use my modules without specifying the path 
Javascript :: node.js wikipedia api call 
Javascript :: npm run watch-poll 
Javascript :: paamayim nekudotayim 
Javascript :: complex type in javascript and memory allocation 
Javascript :: array of in javascript 
Javascript :: fluentmigrator update row where 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =