
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count distinct pairs from two arrays having same sum of digits in C++
We are given with two arrays let’s say, arr_1[] and arr_2[] having integer values and the task is to calculate the count of distinct pairs having the same sum of digits. It means, one value should be selected from an arr_1[] and second value from arr_2[] to form a pair and both the values should have the same sum digits.
Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
For Example
Input − int arr_1[] = {1, 22, 42, 17} Int arr_2[] = {1, 31, 6, 8} Output − count is 4
Explanation − in total there are 4 pairs having the same sum of digits and those are (1, 1), (22, 31), (42, 6) and (17, 8).
Input − int arr_1[] = {1, 22, 42, 17} Int arr_2[] = {2, 78, 6, 18} Output − count is 1
Explanation − in total there is only one pair having the same sum of digits and that is (42, 6).
Input − int arr_1[] = {1, 22, 42, 17} Int arr_2[] = {2, 78, 16, 18} Output − count is 0
Explanation − There is no pair having the same sum of digits so count is 0.
Approach used in the below program is as follows −
Create two arrays let’s say, arr_1[] and arr_2[]
Calculate the length of both the arrays using the length() function that will return an integer value as per the elements in an array.
Create a set type variable let’s say st
Start loop for i to 0 and i less than size of arr_1[]
Inside the loop, Start another loop for j to 0 and j less than size of arr_2[].
Check if Sum(arr[i]) = sum(arr_2[j]) then check if arr_1[i] less than arr_2[j] then insert(make_pair(arr_1[i], arr_2[j])
Else, insert(make_pair(arr_2[j], arr_1[i]).
Return st.size()
Print the result.
Example
#include <iostream> #include <set> using namespace std; // Function to find the // sum of digits of a number int sumdigits(int n){ int sum = 0; while (n > 0){ sum += n % 10; n = n / 10; } return sum; } //function to count the number of pairs int paircount(int arr_1[], int arr_2[], int size1, int size2){ // set is used to avoid duplicate pairs set<pair<int, int> > myset; for (int i = 0; i < size1; i++){ for (int j = 0; j < size2; j++){ // check sum of digits // of both the elements if (sumdigits(arr_1[i]) == sumdigits(arr_2[j])){ if (arr_1[i] < arr_2[j]){ myset.insert(make_pair(arr_1[i], arr_2[j])); } else{ myset.insert(make_pair(arr_2[j], arr_1[i])); } } } } // return size of the set return myset.size(); } // Driver code int main(){ int arr_1[] = { 1, 22, 42, 17 }; int arr_2[] = { 5, 31, 6, 8 }; int size1 = sizeof(arr_1) / sizeof(arr_1[0]); int size2 = sizeof(arr_2) / sizeof(arr_2[0]); cout <<"count is "<<paircount(arr_1, arr_2, size1, size2); return 0; }
Output
If we run the above code we will get the following output &miuns;
count is 3
- Related Articles
- Count pairs from two arrays having sum equal to K in C++
- Count Pairs from two arrays with even sum in C++
- Count pairs formed by distinct element sub-arrays in C++
- Count pairs from two sorted arrays whose sum is equal to a given value x in C++
- Count pairs from two arrays whose modulo operation yields K in C++
- Maximum count of pairs which generate the same sum in C++
- Count subarrays having total distinct elements same as original array in C++
- Count all sub-arrays having sum divisible by k
- Count number of distinct pairs whose sum exists in the given array in C++
- Maximizing Unique Pairs from two arrays in C++
- Sum of special triplets having elements from 3 arrays in C++
- Count of subsequences having maximum distinct elements in C++
- Count subsets having distinct even numbers in C++
- Find Sum of pair from two arrays with maximum sum in C++
- Count pairs from two linked lists whose sum is equal to a given value in C++
