Count all distinct pairs with product equal to K
Given an integer array arr[] of size N and a positive integer K, the task is to count all the distinct pairs in the array with product equal to K.
Examples:
Input: arr[] = {1, 5, 3, 4, 2}, K = 3
Output: 1
Explanation:
There is only one pair (1, 3) with product = K = 3.
Input: arr[] = {1, 2, 16, 4, 4}, K = 16
Output: 2
Explanation:
There are two pairs (1, 16) and (4, 4) with product = K = 16.
Efficient Approach: The idea is to use hashing.
- Initially, insert all the elements from the array into the hashmap. While inserting, ignore if a particular element is already present in the hashmap.
- Now, we have all the unique elements in the hash. So, for every element in the array arr[i], we check if arr[i] / K is present in the hashmap or not.
- If the value is present, then we increment the count and remove that particular element from the hash( To get the unique pairs).
Below is the implementation of the above approach:
C++
// C++ program to count the number of pairs // whose product is equal to K #include <algorithm> #include <iostream> using namespace std; #define MAX 100000 // Function to count the number of pairs // whose product is equal to K int countPairsWithProductK( int arr[], int n, int k) { // Initialize the count int count = 0; // Initialize empty hashmap. bool hashmap[MAX] = { false }; // Insert array elements to hashmap for ( int i = 0; i < n; i++) hashmap[arr[i]] = true ; for ( int i = 0; i < n; i++) { int x = arr[i]; double index = 1.0 * k / arr[i]; // Checking if the index is a whole number // and present in the hashmap if (index >= 0 && ((index - ( int )(index)) == 0) && hashmap[k / x]) count++; hashmap[x] = false ; } return count; } // Driver code int main() { int arr[] = { 1, 5, 3, 4, 2 }; int N = sizeof (arr) / sizeof (arr[0]); int K = 3; cout << countPairsWithProductK(arr, N, K); return 0; } |
Java
// Java program to count the number of pairs // whose product is equal to K class GFG { static int MAX = 100000 ; // Function to count the number of pairs // whose product is equal to K static int countPairsWithProductK( int arr[], int n, int k) { // Initialize the count int count = 0 ; int i; // Initialize empty hashmap. boolean hashmap[] = new boolean [MAX]; // Insert array elements to hashmap for (i = 0 ; i < n; i++) hashmap[arr[i]] = true ; for (i = 0 ; i < n; i++) { int x = arr[i]; double index = 1.0 * k / arr[i]; // Checking if the index is a whole number // and present in the hashmap if (index >= 0 && ((index - ( int )(index)) == 0 ) && hashmap[k / x]) count++; hashmap[x] = false ; } return count; } // Driver code public static void main(String []args) { int arr[] = { 1 , 5 , 3 , 4 , 2 }; int N = arr.length; int K = 3 ; System.out.print(countPairsWithProductK(arr, N, K)); } } |
Python3
# Python3 program to count the number of pairs # whose product is equal to K MAX = 100000 ; # Function to count the number of pairs # whose product is equal to K def countPairsWithProductK(arr, n, k) : # Initialize the count count = 0 ; # Initialize empty hashmap. hashmap = [ False ] * MAX ; # Insert array elements to hashmap for i in range (n) : hashmap[arr[i]] = True ; for i in range (n) : x = arr[i]; index = 1.0 * k / arr[i]; # Checking if the index is a whole number # and present in the hashmap if (index > = 0 and ((index - int (index)) = = 0 ) and hashmap[k / / x]) : count + = 1 ; hashmap[x] = False ; return count; # Driver code if __name__ = = "__main__" : arr = [ 1 , 5 , 3 , 4 , 2 ]; N = len (arr); K = 3 ; print (countPairsWithProductK(arr, N, K)); # This code is contributed by AnkitRai01 |
C#
// C# program to count the number of pairs // whose product is equal to K using System; class GFG { static int MAX = 100000; // Function to count the number of pairs // whose product is equal to K static int countPairsWithProductK( int []arr, int n, int k) { // Initialize the count int count = 0; int i; // Initialize empty hashmap. bool []hashmap = new bool [MAX]; // Insert array elements to hashmap for (i = 0; i < n; i++) hashmap[arr[i]] = true ; for (i = 0; i < n; i++) { int x = arr[i]; double index = 1.0 * k / arr[i]; // Checking if the index is a whole number // and present in the hashmap if (index >= 0 && ((index - ( int )(index)) == 0) && hashmap[k / x]) count++; hashmap[x] = false ; } return count; } // Driver code public static void Main(String []args) { int []arr = { 1, 5, 3, 4, 2 }; int N = arr.Length; int K = 3; Console.Write(countPairsWithProductK(arr, N, K)); } } // This code is contributed by 29AjayKumar |
Javascript
<script> // JavaScript program to count the number of pairs // whose product is equal to K let MAX = 100000; // Function to count the number of pairs // whose product is equal to K function countPairsWithProductK(arr,n,k) { // Initialize the count let count = 0; let i; // Initialize empty hashmap. let hashmap = new Array(MAX); // Insert array elements to hashmap for (i = 0; i < n; i++) hashmap[arr[i]] = true ; for (i = 0; i < n; i++) { let x = arr[i]; let index = 1.0 * k / arr[i]; // Checking if the index is a whole number // and present in the hashmap if (index >= 0 && ((index - Math.floor(index)) == 0) && hashmap[k / x]) count++; hashmap[x] = false ; } return count; } // Driver code let arr=[1, 5, 3, 4, 2]; let N = arr.length; let K = 3; document.write(countPairsWithProductK(arr, N, K)); // This code is contributed by rag2127 </script> |
Output:
1
Time Complexity: O(N)
Auxiliary Space: O(MAX), since MAX space has been taken.
Please Login to comment...