📌  相关文章
📜  计算所有不重复对的乘积等于K

📅  最后修改于: 2021-04-29 06:19:12             🧑  作者: Mango

给定大小为N的整数数组arr []和正整数K ,任务是计算乘积等于K的数组中所有不同的对。
例子:

幼稚的方法:针对此问题的幼稚的方法是一对一地考虑所有对,并检查每对之间的乘积。如果乘积等于K ,则增加计数并最终打印出来。但是,局限性在于,如果数组中存在重复项,则此方法将不起作用,因为我们只需要考虑不同的对即可。
下面是上述方法的实现:

CPP
// C++ program to count the number of pairs
// whose product is equal to K
 
#include 
using namespace std;
 
// Function to count the number of pairs
// whose product is equal to K
int countPairsWithProdK(int arr[], int n, int k)
{
    int count = 0;
 
    // Pick all elements one by one
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++)
 
            // Check if the product of this pair
            // is equal to K
            if (arr[i] * arr[j] == k)
                count++;
    }
 
    return count;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 5, 3, 4, 2 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int K = 3;
 
    cout << countPairsWithProdK(arr, N, K);
    return 0;
}


Java
// Java program to count the number of pairs
// whose product is equal to K
class GFG {
 
     // Function to count the number of pairs
    // whose product is equal to K
    static int countPairsWithProdK(int arr[], int n, int k)
    {
        int count = 0;
     
        // Pick all elements one by one
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++)
     
                // Check if the product of this pair
                // is equal to K
                if (arr[i] * arr[j] == k)
                    count++;
        }
     
        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.println(countPairsWithProdK(arr, N, K));
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 program to count the number of pairs
# whose product is equal to K
 
# Function to count the number of pairs
# whose product is equal to K
def countPairsWithProdK(arr, n, k):
    count = 0
 
    # Pick all elements one by one
    for i in range(n):
        for j in range(i + 1, n):
 
            # Check if the product of this pair
            # is equal to K
            if (arr[i] * arr[j] == k):
                count += 1
 
    return count
 
# Driver code
arr = [1, 5, 3, 4, 2]
N = len(arr)
K = 3
 
print(countPairsWithProdK(arr, N, K))
 
# This code is contributed by mohit kumar 29


C#
// C# program to count the number of pairs
// whose product is equal to K
using System;
 
class GFG {
 
     // Function to count the number of pairs
    // whose product is equal to K
    static int countPairsWithProdK(int []arr, int n, int k)
    {
        int count = 0;
     
        // Pick all elements one by one
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++)
     
                // Check if the product of this pair
                // is equal to K
                if (arr[i] * arr[j] == k)
                    count++;
        }
     
        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.WriteLine(countPairsWithProdK(arr, N, K));
    }
}
 
// This code is contributed by AnkitRai01


Javascript


C++
// C++ program to count the number of pairs
// whose product is equal to K
 
#include 
#include 
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


输出:
1

时间复杂度: O(N 2 )
高效的方法:想法是使用哈希。

  1. 最初,将数组中的所有元素插入到哈希图中。插入时,请忽略哈希图中是否已存在特定元素。
  2. 现在,我们在哈希表中拥有所有唯一元素。因此,对于数组arr [i]中的每个元素,我们都会检查哈希图中是否存在arr [i] / K。
  3. 如果存在该值,则我们增加计数并从哈希中删除该特定元素(以获取唯一对)。

下面是上述方法的实现:

C++

// C++ program to count the number of pairs
// whose product is equal to K
 
#include 
#include 
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
输出:
1

时间复杂度: O(N * log(N))