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++
// 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; } |
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<Integer> s1 = new HashSet<Integer>(); 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<pair> s2 = new HashSet<pair>(); 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[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< 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<pair> s2 = new HashSet<pair>(); 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
<script> // JavaScript implementation of the approach // Function to get the nth ugly number function uglyNumber(n) { // To store ugly numbers let ugly = new Array(n); let i2 = 0, i3 = 0, i5 = 0; let next_multiple_of_2 = 2; let next_multiple_of_3 = 3; let next_multiple_of_5 = 5; let next_ugly_no = 1; ugly[0] = 1; for (let 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 function totalPairs(arr1, arr2, n, m) { let s1 = new Set(); let i = 1; // Insert ugly numbers in set // which are less than 1000 while (1) { let next_ugly_number = uglyNumber(i); if (next_ugly_number > 1000) break ; s1.add(next_ugly_number); i++; } // Set is used to avoid duplicate pairs let s2 = new Set(); for (let i = 0; i < n; i++) { // Check if arr1[i] is an ugly number if (s1.has(arr1[i])) { for (let j = 0; j < m; j++) { // Check if arr2[i] is an ugly number if (s1.has(arr2[j])) { 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 s2.size; } // Driver code let arr1 = [3, 7, 1]; let arr2 = [5, 1, 10, 4]; let n = arr1.length; let m = arr2.length; document.write(totalPairs(arr1, arr2, n, m)); // This code is contributed by _saurabh_jaiswal </script> |
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)
METHOD 2:Using brute force method
APPROACH:
This program takes two arrays of integers as input, and then finds the total number of distinct pairs from these two arrays, where both numbers in the pair are “ugly” numbers. An ugly number is defined as a number whose only prime factors are 2, 3 or 5.
ALGORITHM:
1.Define a function is_ugly that checks whether a number is an ugly number or not.
2.Take two arrays as input and define a variable count as 0 to keep track of the count of pairs.
3.Loop through each element of arr1 and arr2 using nested for loops.
4.Multiply each element of arr1 with each element of arr2 and check whether their product is an ugly number or not using the is_ugly function.
5.If the product is an ugly number, increment the count variable.
6.Finally, print the count variable, which represents the total number of distinct pairs where both numbers in the pair are ugly numbers.
C++
#include <iostream> using namespace std; // Function to check if a number is ugly bool isUgly( int n) { while (n % 2 == 0) { n /= 2; } while (n % 3 == 0) { n /= 3; } while (n % 5 == 0) { n /= 5; } return n == 1; } int main() { int arr1[] = { 3, 7, 1 }; int arr2[] = { 5, 1, 10, 4 }; int count = 0; // Iterate over the elements of both arrays for ( int i = 0; i < sizeof (arr1) / sizeof (arr1[0]); i++) { for ( int j = 0; j < sizeof (arr2) / sizeof (arr2[0]); j++) { // Check if the product of the elements is an ugly number if (isUgly(arr1[i] * arr2[j])) { count++; } } } cout << count << endl; return 0; } |
Java
import java.util.Arrays; // Function to check if a number is ugly class Main { public static boolean isUgly( int n) { while (n % 2 == 0 ) { n /= 2 ; } while (n % 3 == 0 ) { n /= 3 ; } while (n % 5 == 0 ) { n /= 5 ; } return n == 1 ; } public static void main(String[] args) { int [] arr1 = { 3 , 7 , 1 }; int [] arr2 = { 5 , 1 , 10 , 4 }; int count = 0 ; // Iterate over the elements of both arrays for ( int i = 0 ; i < arr1.length; i++) { for ( int j = 0 ; j < arr2.length; j++) { // Check if the product of the elements is // an ugly number if (isUgly(arr1[i] * arr2[j])) { count++; } } } System.out.println(count); } } |
Python3
def is_ugly(n): while n % 2 = = 0 : n / / = 2 while n % 3 = = 0 : n / / = 3 while n % 5 = = 0 : n / / = 5 return n = = 1 arr1 = [ 3 , 7 , 1 ] arr2 = [ 5 , 1 , 10 , 4 ] count = 0 for i in range ( len (arr1)): for j in range ( len (arr2)): if is_ugly(arr1[i] * arr2[j]): count + = 1 print (count) |
C#
using System; class Program { // Function to check if a number is ugly static bool IsUgly( int n) { while (n % 2 == 0) { n /= 2; } while (n % 3 == 0) { n /= 3; } while (n % 5 == 0) { n /= 5; } return n == 1; } static void Main( string [] args) { int [] arr1 = { 3, 7, 1 }; int [] arr2 = { 5, 1, 10, 4 }; int count = 0; // Iterate over the elements of both arrays for ( int i = 0; i < arr1.Length; i++) { for ( int j = 0; j < arr2.Length; j++) { // Check if the product of the elements is // an ugly number if (IsUgly(arr1[i] * arr2[j])) { count++; } } } Console.WriteLine(count); } } |
Javascript
// Function to check if a number is an ugly number function is_ugly(n) { while (n % 2 == 0) { n /= 2; } while (n % 3 == 0) { n /= 3; } while (n % 5 == 0) { n /= 5; } return n == 1; } let arr1 = [3, 7, 1]; let arr2 = [5, 1, 10, 4]; let count = 0; // Iterate over the elements of both arrays for (let i = 0; i < arr1.length; i++) { for (let j = 0; j < arr2.length; j++) { // Check if the product of the elements is an ugly number if (is_ugly(arr1[i] * arr2[j])) { count += 1; } } } console.log(count); // This code is contributed by Kanchan Agarwal |
8
Time complexity: O(N^2), where N is the maximum length of the input arrays.
Auxiliary Space: O(1), as we are not using any extra space in the program apart from the input arrays and the count variable.
Contact Us