Given two arrays arr1[] and arr2[]. The task is to find the total number of distinct pairs(formed by picking 1 element from arr1 and one element from arr2), such that both the elements of the pair have the sum of digits.
Note: Pairs occurring more than once must be counted only once.
Examples:
Input : arr1[] = {33, 41, 59, 1, 3}
arr2[] = {3, 32, 51, 3}
Output : 3
Possible pairs are:
(33, 51), (41, 32), (3, 3)
Input : arr1[] = {1, 6, 4, 22}
arr2[] = {1, 3, 24}
Output : 2
Possible pairs are:
(1, 1), (6, 24)
Approach:
- Run two nested loops to generate all possible pairs from the two arrays taking one element from arr1[] and one from arr2[].
- If sum of digits is equal, then insert the pair(a, b) into a set, in order to avoid duplicates where a is the smaller element and b is the larger one.
- Total pairs will be the size of the final set.
Below is the implementation of the above approach:
C++
// C++ program to count total number of // pairs having elements with same // sum of digits #include <bits/stdc++.h> using namespace std; // Function for returning // sum of digits of a number int digitSum(int n) { int sum = 0; while (n > 0) { sum += n % 10; n = n / 10; } return sum; } // Function to return the total pairs // of elements with equal sum of digits int totalPairs(int arr1[], int arr2[], int n, int m) { // set is used to avoid duplicate pairs set<pair<int, int> > s; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { // check sum of digits // of both the elements if (digitSum(arr1[i]) == digitSum(arr2[j])) { if (arr1[i] < arr2[j]) s.insert(make_pair(arr1[i], arr2[j])); else s.insert(make_pair(arr2[j], arr1[i])); } } } // return size of the set return s.size(); } // Driver code int main() { int arr1[] = { 100, 3, 7, 50 }; int arr2[] = { 5, 1, 10, 4 }; int n = sizeof(arr1) / sizeof(arr1[0]); int m = sizeof(arr2) / sizeof(arr2[0]); cout << totalPairs(arr1, arr2, n, m); return 0; } |
Java
// Java program to count total number of // pairs having elements with same // sum of digits import java.util.*; class GFG { static class pair { int first, second; public pair(int first, int second) { this.first = first; this.second = second; } } // Function for returning // sum of digits of a number static int digitSum(int n) { int sum = 0; while (n > 0) { sum += n % 10; n = n / 10; } return sum; } // Function to return the total pairs // of elements with equal sum of digits static int totalPairs(int arr1[], int arr2[], int n, int m) { // set is used to avoid duplicate pairs Set<pair> s = new HashSet<>(); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { // check sum of digits // of both the elements if (digitSum(arr1[i]) == digitSum(arr2[j])) { if (arr1[i] < arr2[j]) s.add(new pair(arr1[i], arr2[j])); else s.add(new pair(arr2[j], arr1[i])); } } } // return size of the set return s.size(); } // Driver code public static void main(String[] args) { int arr1[] = { 100, 3, 7, 50 }; int arr2[] = { 5, 1, 10, 4 }; int n = arr1.length; int m = arr2.length; System.out.println(totalPairs(arr1, arr2, n, m)); } } // This code is contributed by Rajput-Ji |
Python3
# Python3 program to count total number of # pairs having elements with same sum of digits # Function for returning # sum of digits of a number def digitSum(n): Sum = 0 while n > 0: Sum += n % 10 n = n // 10 return Sum # Function to return the total pairs # of elements with equal sum of digits def totalPairs(arr1, arr2, n, m): # set is used to avoid duplicate pairs s = set() for i in range(0, n): for j in range(0, m): # check sum of digits # of both the elements if digitSum(arr1[i]) == digitSum(arr2[j]): if arr1[i] < arr2[j]: s.add((arr1[i], arr2[j])) else: s.add((arr2[j], arr1[i])) # return size of the set return len(s) # Driver code if __name__ == "__main__": arr1 = [100, 3, 7, 50] arr2 = [5, 1, 10, 4] n = len(arr1) m = len(arr2) print(totalPairs(arr1, arr2, n, m)) # This code is contributed by Rituraj Jain |
C#
// C# program to count total number of // pairs having elements with same // sum of digits using System; using System.Collections.Generic; class GFG { public class pair { public int first, second; public pair(int first, int second) { this.first = first; this.second = second; } } // Function for returning // sum of digits of a number static int digitSum(int n) { int sum = 0; while (n > 0) { sum += n % 10; n = n / 10; } return sum; } // Function to return the total pairs // of elements with equal sum of digits static int totalPairs(int []arr1, int []arr2, int n, int m) { // set is used to avoid duplicate pairs HashSet<pair> s = new HashSet<pair>(); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { // check sum of digits // of both the elements if (digitSum(arr1[i]) == digitSum(arr2[j])) { if (arr1[i] < arr2[j]) s.Add(new pair(arr1[i], arr2[j])); else s.Add(new pair(arr2[j], arr1[i])); } } } // return size of the set return s.Count; } // Driver code public static void Main(String[] args) { int []arr1 = { 100, 3, 7, 50 }; int []arr2 = { 5, 1, 10, 4 }; int n = arr1.Length; int m = arr2.Length; Console.WriteLine(totalPairs(arr1, arr2, n, m)); } } // This code is contributed by Princi Singh |
3
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count of pairs between two arrays such that the sums are distinct
- Count pairs formed by distinct element sub-arrays
- Distinct pairs from given arrays (a[i], b[j]) such that (a[i] + b[j]) is a Fibonacci number
- Total distinct pairs of ugly numbers from two arrays
- Total distinct pairs from two arrays such that second number can be obtained by inverting bits of first
- Count all distinct pairs with difference equal to k
- Count all distinct pairs with product equal to K
- Count numbers with exactly K non-zero digits and distinct odd digit sum
- Count number of distinct pairs whose sum exists in the given array
- Count of distinct pair sum between two 1 to N value Arrays
- Count Pairs from two arrays with even sum
- Given two arrays count all pairs whose sum is an odd number
- Count of distinct sums that can be obtained by adding prime numbers from given arrays
- Count equal pairs from given string arrays
- Count pairs from two sorted arrays whose sum is equal to a given value x
- Max count of unique ratio/fraction pairs in given arrays
- Count pairs from two arrays whose modulo operation yields K
- Count of distinct possible pairs such that the element from A is greater than the element from B
- Count of pairs from arrays A and B such that element in A is greater than element in B at that index
- Count of numbers upto N digits formed using digits 0 to K-1 without any adjacent 0s
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

