# Total distinct pairs of ugly numbers from two arrays

Given two arrays **arr1[]** and **arr2[]** of sizes **N** and **M** where **0 ≤ arr1[i], arr2[i] ≤ 1000** for all valid **i**, the task is to take one element from first array and one element from second array such that both of them are ugly numbers. We call it a pair (a, b). You have to find the count of all such distinct pairs. **Note** that (a, b) and (b, a) are not distinct.

Ugly numbers are numbers whose only prime factors are **2, 3 or 5**.

The sequence **1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, …..** shows first few ugly numbers. By convention, 1 is included.

**Examples:**

Input:arr1[] = {7, 2, 3, 14}, arr2[] = {2, 11, 10}

Output:4

All distinct pairs are (2, 2), (2, 10), (3, 2) and (3, 10)

Input:arr1[] = {1, 2, 3}, arr2[] = {1, 1}

Output:3

All distinct pairs are (1, 1), (1, 2) and (1, 3)

**Approach:**

- First generate all ugly numbers and insert them in a unordered_set s1.
- Take another empty set s2.
- Run two nested loops to generate all possible pairs from the two arrays taking one element from first array(call it a) and one from second array(call it b).
- Check if a is present in s1. If yes then check for each element of arr2[] if it is also present in s1.
- If both a and b are ugly numbers, then insert pair (a, b) in s2 if a is less than b, or (b, a) otherwise. This is done to avoid duplicacy.
- Total pairs is the size of the set s2.

Below is the implementation of the above approach:

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to get the nth ugly number ` `unsigned uglyNumber(` `int` `n) ` `{ ` ` ` ` ` `// To store ugly numbers ` ` ` `int` `ugly[n]; ` ` ` `int` `i2 = 0, i3 = 0, i5 = 0; ` ` ` `int` `next_multiple_of_2 = 2; ` ` ` `int` `next_multiple_of_3 = 3; ` ` ` `int` `next_multiple_of_5 = 5; ` ` ` `int` `next_ugly_no = 1; ` ` ` ` ` `ugly[0] = 1; ` ` ` `for` `(` `int` `i = 1; i < n; i++) { ` ` ` `next_ugly_no = min(next_multiple_of_2, ` ` ` `min(next_multiple_of_3, ` ` ` `next_multiple_of_5)); ` ` ` `ugly[i] = next_ugly_no; ` ` ` `if` `(next_ugly_no == next_multiple_of_2) { ` ` ` `i2 = i2 + 1; ` ` ` `next_multiple_of_2 = ugly[i2] * 2; ` ` ` `} ` ` ` `if` `(next_ugly_no == next_multiple_of_3) { ` ` ` `i3 = i3 + 1; ` ` ` `next_multiple_of_3 = ugly[i3] * 3; ` ` ` `} ` ` ` `if` `(next_ugly_no == next_multiple_of_5) { ` ` ` `i5 = i5 + 1; ` ` ` `next_multiple_of_5 = ugly[i5] * 5; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `next_ugly_no; ` `} ` ` ` `// Function to return the required count of pairs ` `int` `totalPairs(` `int` `arr1[], ` `int` `arr2[], ` `int` `n, ` `int` `m) ` `{ ` ` ` `unordered_set<` `int` `> s1; ` ` ` `int` `i = 1; ` ` ` ` ` `// Insert ugly numbers in set ` ` ` `// which are less than 1000 ` ` ` `while` `(1) { ` ` ` `int` `next_ugly_number = uglyNumber(i); ` ` ` `if` `(next_ugly_number > 1000) ` ` ` `break` `; ` ` ` `s1.insert(next_ugly_number); ` ` ` `i++; ` ` ` `} ` ` ` ` ` `// Set is used to avoid duplicate pairs ` ` ` `set<pair<` `int` `, ` `int` `> > s2; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// Check if arr1[i] is an ugly number ` ` ` `if` `(s1.find(arr1[i]) != s1.end()) { ` ` ` ` ` `for` `(` `int` `j = 0; j < m; j++) { ` ` ` ` ` `// Check if arr2[i] is an ugly number ` ` ` `if` `(s1.find(arr2[j]) != s1.end()) { ` ` ` `if` `(arr1[i] < arr2[j]) ` ` ` `s2.insert(make_pair(arr1[i], arr2[j])); ` ` ` `else` ` ` `s2.insert(make_pair(arr2[j], arr1[i])); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return the size of the set s2 ` ` ` `return` `s2.size(); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr1[] = { 3, 7, 1 }; ` ` ` `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; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

8

## Recommended Posts:

- Total distinct pairs from two arrays such that second number can be obtained by inverting bits of first
- Count distinct pairs from two arrays having same sum of digits
- Distinct pairs from given arrays (a[i], b[j]) such that (a[i] + b[j]) is a Fibonacci number
- Count pairs formed by distinct element sub-arrays
- Ugly Numbers
- Count of distinct sums that can be obtained by adding prime numbers from given arrays
- Maximum length of a sub-array with ugly numbers
- Sort ugly numbers in an array at their relative positions
- Find total number of distinct years from a string
- Count subarrays having total distinct elements same as original array
- Total character pairs from two strings, with equal number of set bits in their ascii value
- Count all distinct pairs with difference equal to k
- Count number of distinct pairs whose sum exists in the given array
- Print all distinct integers that can be formed by K numbers from a given array of N numbers
- Count Pairs from two arrays with even sum

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.