📜  计算乘积等于 K 的所有不同对

📅  最后修改于: 2021-09-04 08:21:44             🧑  作者: 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


Javascript


输出:

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));
         
    }
}

蟒蛇3

# 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


输出:
1

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