# Total distinct pairs of ugly numbers from two arrays

• Difficulty Level : Expert
• Last Updated : 09 Jun, 2022

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:
All distinct pairs are (2, 2), (2, 10), (3, 2) and (3, 10)
Input: arr1[] = {1, 2, 3}, arr2[] = {1, 1}
Output:
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++

 `// C++ implementation of the approach``#include ``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 = 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 > 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);``    ``int` `m = ``sizeof``(arr2) / ``sizeof``(arr2);` `    ``cout << totalPairs(arr1, arr2, n, m);` `    ``return` `0;``}`

## Java

 `// Java implementation of the approach``import` `java.util.*;` `class` `GFG``{``static` `class` `pair``{``    ``int` `first, second;``    ``public` `pair(``int` `first, ``int` `second)``    ``{``        ``this``.first = first;``        ``this``.second = second;``    ``}``}` `// Function to get the nth ugly number``static` `int` `uglyNumber(``int` `n)``{` `    ``// To store ugly numbers``    ``int` `[]ugly = ``new` `int``[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 = Math.min(next_multiple_of_2,``                       ``Math.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``static` `int` `totalPairs(``int` `arr1[], ``int` `arr2[],``                      ``int` `n, ``int` `m)``{``    ``HashSet s1 = ``new` `HashSet();``    ``int` `i = ``1``;` `    ``// Insert ugly numbers in set``    ``// which are less than 1000``    ``while` `(``true``)``    ``{``        ``int` `next_ugly_number = uglyNumber(i);``        ``if` `(next_ugly_number > ``1000``)``            ``break``;``        ``s1.add(next_ugly_number);``        ``i++;``    ``}` `    ``// Set is used to avoid duplicate pairs``    ``HashSet s2 = ``new` `HashSet();` `    ``for` `(i = ``0``; i < n; i++)``    ``{` `        ``// Check if arr1[i] is an ugly number``        ``if` `(s1.contains(arr1[i]))``        ``{``            ``for` `(``int` `j = ``0``; j < m; j++)``            ``{` `                ``// Check if arr2[i] is an ugly number``                ``if` `(s1.contains(arr2[j]))``                ``{``                    ``if` `(arr1[i] < arr2[j])``                        ``s2.add(``new` `pair(arr1[i], arr2[j]));``                    ``else``                        ``s2.add(``new` `pair(arr2[j], arr1[i]));``                ``}``            ``}``        ``}``    ``}` `    ``// Return the size of the set s2``    ``return` `s2.size();``}` `// Driver code``public` `static` `void` `main(String[] args)``{``    ``int` `arr1[] = { ``3``, ``7``, ``1` `};``    ``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 PrinciRaj1992`

## Python3

 `# Python3 implementation of the approach` `# Function to get the nth ugly number``def` `uglyNumber(n):` `    ``# To store ugly numbers``    ``ugly ``=` `[``None``] ``*` `n``    ``i2 ``=` `i3 ``=` `i5 ``=` `0``    ``next_multiple_of_2 ``=` `2``    ``next_multiple_of_3 ``=` `3``    ``next_multiple_of_5 ``=` `5``    ``next_ugly_no ``=` `1` `    ``ugly[``0``] ``=` `1``    ``for` `i ``in` `range``(``1``, n): ``        ``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``def` `totalPairs(arr1, arr2, n, m):`` ` `    ``s1 ``=` `set``()``    ``i ``=` `1` `    ``# Insert ugly numbers in set``    ``# which are less than 1000``    ``while` `True``: ``        ``next_ugly_number ``=` `uglyNumber(i)``        ``if` `(next_ugly_number > ``1000``):``            ``break``        ``s1.add(next_ugly_number)``        ``i ``+``=` `1``     ` `    ``# Set is used to avoid duplicate pairs``    ``s2 ``=` `set``()` `    ``for` `i ``in` `range``(``0``, n): ` `        ``# Check if arr1[i] is an ugly number``        ``if` `arr1[i] ``in` `s1: ` `            ``for` `j ``in` `range``(``0``, m): ` `                ``# Check if arr2[i] is an ugly number``                ``if` `arr2[j] ``in` `s1: ``                    ``if` `(arr1[i] < arr2[j]):``                        ``s2.add((arr1[i], arr2[j]))``                    ``else``:``                        ``s2.add((arr2[j], arr1[i]))` `    ``# Return the size of the set s2``    ``return` `len``(s2)`` ` `# Driver code``if` `__name__ ``=``=` `"__main__"``:`` ` `    ``arr1 ``=` `[``3``, ``7``, ``1``] ``    ``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# implementation of the approach``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 to get the nth ugly number``static` `int` `uglyNumber(``int` `n)``{` `    ``// To store ugly numbers``    ``int` `[]ugly = ``new` `int``[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 = 1;``    ``for` `(``int` `i = 1; i < n; i++)``    ``{``        ``next_ugly_no = Math.Min(next_multiple_of_2,``                       ``Math.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``static` `int` `totalPairs(``int` `[]arr1, ``int` `[]arr2,``                      ``int` `n, ``int` `m)``{``    ``HashSet<``int``> s1 = ``new` `HashSet<``int``>();``    ``int` `i = 1;` `    ``// Insert ugly numbers in set``    ``// which are less than 1000``    ``while` `(``true``)``    ``{``        ``int` `next_ugly_number = uglyNumber(i);``        ``if` `(next_ugly_number > 1000)``            ``break``;``        ``s1.Add(next_ugly_number);``        ``i++;``    ``}` `    ``// Set is used to avoid duplicate pairs``    ``HashSet s2 = ``new` `HashSet();` `    ``for` `(i = 0; i < n; i++)``    ``{` `        ``// Check if arr1[i] is an ugly number``        ``if` `(s1.Contains(arr1[i]))``        ``{``            ``for` `(``int` `j = 0; j < m; j++)``            ``{` `                ``// Check if arr2[i] is an ugly number``                ``if` `(s1.Contains(arr2[j]))``                ``{``                    ``if` `(arr1[i] < arr2[j])``                        ``s2.Add(``new` `pair(arr1[i],``                                        ``arr2[j]));``                    ``else``                        ``s2.Add(``new` `pair(arr2[j],``                                        ``arr1[i]));``                ``}``            ``}``        ``}``    ``}` `    ``// Return the size of the set s2``    ``return` `s2.Count;``}` `// Driver code``public` `static` `void` `Main(String[] args)``{``    ``int` `[]arr1 = { 3, 7, 1 };``    ``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 Rajput-Ji`

## Javascript

 ``

Output:

`8`

Time Complexity: O(k2+n*m*log(n+m)) where k = 1000 and n & m are the sizes of the array
Auxiliary Space: O(k+n+m)

My Personal Notes arrow_drop_up