- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count pairs from two sorted arrays whose sum is equal to a given value x in C++
We are given two arrays containing positive numbers and a value x. The goal is to find pairs of elements of arrays such that pairs of type (A, B) has A+B=x and A belongs to the first array and B belongs to the second array.
Let us understand with examples
Input − arr_1[] = {1,2,5,3,4}; arr_2[] = {7,0,1,3}; x=6
Output −Count of pairs from two sorted arrays whose sum is equal to a given value x are − 2
Explanation − The pairs are (5,1) - (arr_1[2],arr_2[2]) and (3,3) - (arr_1[3],arr_2[3])
Input − arr_1[] = {1,1,1}; arr_2[] = {2,2}; x=6
Output − Count of pairs from two sorted arrays whose sum is equal to a given value x are − 2
Explanation − The pairs are (1,2) - (arr_1[0],arr_2[0]) and (1,2) - (arr_1[1],arr_2[1])
Approach used in the below program is as follows
We will use two approaches. First naive approach using a for loop. Start traversing both using for loops such that index i is for arr_1[] and index j is for arr_2[]. For pair (arr_1[i]+arr_2[j]==x), increment count. Return count as result.
Take an integer arrays arr_1[] and arr_2[] with positive elements and lengths as size_arr_1 and size_arr_2.
Function Pair_value_x(int arr_1[], int arr_2[], int size_arr_1, int size_arr_2, int x) takes both arrays and their lengths and returns the pairs such that the sum of elements is x.
Take the initial value of count as 0.
Start traversing arr_1[] from i=0 to i<size_arr_1 and arr_2[] from j=0 to j
For each pair arr_1[i], arr_2[j], check if sum is x. If true, increment count.
Return count as result.
Efficient Approach
In this approach we will create an unordered_set of elements of arr_1. Now traverse arr_2 using for loop and for each value arr_2[i], if x-arr_2[i] is found in the set then increment count. Return count at the end.
Take the same arrays and their sizes.
Function Pair_value_x(int arr_1[], int arr_2[], int size_arr_1, int size_arr_2, int x) takes both arrays and their lengths and returns the pairs such that the sum of elements is x.
Take the initial count as 0.
Create unordered_set<int> hash_map for storing unique elements of arr_1.
Populate hash_map with elements of arr_1 using for loop.
Now traverse arr_2[] using for loop.
For each arr-2[j], if x-arr_2[j] is found in hash_map using (hash_map.find(x - arr_2[j]) != hash_map.end()), then increment count.
At the end count as pairs with sum equals x.
Return count as result.
Example (naive approach)
#include <bits/stdc++.h> using namespace std; int Pair_value_x(int arr_1[], int arr_2[], int size_arr_1, int size_arr_2, int x){ int count = 0; for (int i = 0; i < size_arr_1; i++){ for (int j = 0; j < size_arr_2; j++){ if ((arr_1[i] + arr_2[j]) == x){ count++; } } } return count; } int main(){ int arr_1[] = {1, 2, 3, 4}; int arr_2[] = {2, 3, 4, 5}; int size_arr_1 = sizeof(arr_1) / sizeof(arr_1[0]); int size_arr_2 = sizeof(arr_2) / sizeof(arr_2[0]); int x = 6; cout<<"Count of pairs from two sorted arrays whose sum is equal to a given value x are: "<< Pair_value_x(arr_1, arr_2, size_arr_1 , size_arr_2, x); return 0; }
Output
If we run the above code it will generate the following output −
Count of pairs from two sorted arrays whose sum is equal to a given value x are: 4
Example (Efficient Approach)
#include <bits/stdc++.h> using namespace std; int Pair_value_x(int arr_1[], int arr_2[], int size_arr_1, int size_arr_2, int x){ int count = 0; unordered_set<int> hash_map; for (int i = 0; i < size_arr_1; i++){ hash_map.insert(arr_1[i]); } for (int j = 0; j < size_arr_2; j++){ if (hash_map.find(x - arr_2[j]) != hash_map.end()){ count++; } } return count; } int main(){ int arr_1[] = {1, 2, 3, 4}; int arr_2[] = {2, 3, 4, 5}; int size_arr_1 = sizeof(arr_1) / sizeof(arr_1[0]); int size_arr_2 = sizeof(arr_2) / sizeof(arr_2[0]); int x = 6; cout<<"Count of pairs from two sorted arrays whose sum is equal to a given value x are: "<< Pair_value_x(arr_1, arr_2, size_arr_1 , size_arr_2, x); return 0; }
Output
If we run the above code it will generate the following output −
Count of pairs from two sorted arrays whose sum is equal to a given value x are: 4
- Related Articles
- Count quadruples from four sorted arrays whose sum is equal to a given value x in C++
- Count pairs from two BSTs whose sum is equal to a given value x in C++
- Count pairs from two linked lists whose sum is equal to a given value in C++
- Count pairs in a binary tree whose sum is equal to a given value x in C++
- Count pairs from two linked lists whose product is equal to a given value in C++
- Count triplets in a sorted doubly linked list whose sum is equal to a given value x in C++
- Count pairs from two arrays having sum equal to K in C++
- Count pairs in a sorted array whose sum is less than x in C++
- Count triplets in a sorted doubly linked list whose product is equal to a given value x in C++
- Count Pairs from two arrays with even sum in C++
- Count pairs from two arrays whose modulo operation yields K in C++
- Count distinct pairs from two arrays having same sum of digits in C++
- Count numbers whose sum with x is equal to XOR with x in C++
- Count pairs in a sorted array whose product is less than k in C++
- C++ program to find two numbers from two arrays whose sum is not present in both arrays
