📜  一对乘积等于其余一对乘积的四倍计数

📅  最后修改于: 2021-06-25 09:59:59             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是计算数组中唯一四倍体(a,b,c,d)的数量,以使该四倍体中任意一对元素的乘积等于n的乘积。剩下的一对元素。

例子:

天真的方法:最简单的方法是使用四个嵌套循环从给定的数组生成所有可能的四元组,以针对遇到的每个唯一的四元组,检查是否满足给定条件。最后,打印获得的此类三胞胎的计数。

时间复杂度: O(N 4 )
辅助空间: O(1)

高效方法:为了优化上述方法,我们的想法是使用哈希。要解决此问题,请将每个不同对的乘积存储在Hashmap M中

请按照以下步骤解决问题:

  • res初始化为0以存储生成的四元组的计数。
  • 创建一个哈希图M来存储数组中不同对的乘积的频率。
  • 现在,生成给定数组的所有可能的不同对(arr [i],arr [j])并执行以下操作:
    • arr [i]arr [j]的乘积存储在变量prod中
    • (8 * M [prod])的值添加到变量res
    • 将哈希图Mprod的频率增加1
  • 完成上述步骤后,输出res的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count the number of
// unique quadruples from an array
// that satisfies the given condition
void sameProductQuadruples(int nums[],
                           int N)
{
    // Hashmap to store
    // the product of pairs
    unordered_map umap;
 
    // Store the count of
    // required quadruples
    int res = 0;
 
    // Traverse the array arr[] and
    // generate all possible pairs
    for (int i = 0; i < N; ++i) {
        for (int j = i + 1; j < N; ++j) {
 
            // Store their product
            int prod = nums[i] * nums[j];
 
            // Pair(a, b) can be used to
            // generate 8 unique permutations
            // with another pair (c, d)
            res += 8 * umap[prod];
 
            // Increment um[prod] by 1
            ++umap[prod];
        }
    }
 
    // Print the result
    cout << res;
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 3, 4, 6 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    sameProductQuadruples(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG
{
   
// Function to count the number of
// unique quadruples from an array
// that satisfies the given condition
static void sameProductQuadruples(int[] nums,
                           int N)
{
    // Hashmap to store
    // the product of pairs
    int[] umap = new int[10000];
     
    // Store the count of
    // required quadruples
    int res = 0;
 
    // Traverse the array arr[] and
    // generate all possible pairs
    for (int i = 0; i < N; ++i)
    {
        for (int j = i + 1; j < N; ++j)
        {
 
            // Store their product
            int prod = nums[i] * nums[j];
 
            // Pair(a, b) can be used to
            // generate 8 unique permutations
            // with another pair (c, d)
            res += 8 * umap[prod];
 
            // Increment um[prod] by 1
            ++umap[prod];
        }
    }
 
    // Print the result
     System.out.println(res);
}
 
 
// Driver Code
public static void main(String[] args)
{
    int[] arr = { 2, 3, 4, 6 };
    int N = arr.length;
 
    sameProductQuadruples(arr, N);
}
}
 
// This code is contributed by code_hunt.


Python3
# Python program for the above approach
 
# Function to count the number of
# unique quadruples from an array
# that satisfies the given condition
def sameProductQuadruples(nums, N) :
 
    # Hashmap to store
    # the product of pairs
    umap = {};
 
    # Store the count of
    # required quadruples
    res = 0;
 
    # Traverse the array arr[] and
    # generate all possible pairs
    for i in range(N) :
        for j in range(i + 1, N) :
 
            # Store their product
            prod = nums[i] * nums[j];   
            if prod in umap :
                 
                # Pair(a, b) can be used to
                # generate 8 unique permutations
                # with another pair (c, d)
                res += 8 * umap[prod];
     
                # Increment umap[prod] by 1
                umap[prod] += 1;
            else:
                umap[prod] = 1
 
    # Print the result
    print(res);
 
# Driver Code
if __name__ == "__main__" :
 
    arr = [ 2, 3, 4, 6 ];
    N = len(arr);
 
    sameProductQuadruples(arr, N);
 
    # This code is contributed by AnkThon


C#
// C# program to implement
// the above approach
using System;
 
class GFG
{
 
// Function to count the number of
// unique quadruples from an array
// that satisfies the given condition
static void sameProductQuadruples(int[] nums,
                           int N)
{
    // Hashmap to store
    // the product of pairs
    int[] umap = new int[10000];
     
    // Store the count of
    // required quadruples
    int res = 0;
 
    // Traverse the array arr[] and
    // generate all possible pairs
    for (int i = 0; i < N; ++i)
    {
        for (int j = i + 1; j < N; ++j)
        {
 
            // Store their product
            int prod = nums[i] * nums[j];
 
            // Pair(a, b) can be used to
            // generate 8 unique permutations
            // with another pair (c, d)
            res += 8 * umap[prod];
 
            // Increment um[prod] by 1
            ++umap[prod];
        }
    }
 
    // Print the result
    Console.Write(res);
}
 
// Driver Code
public static void Main(String[] args)
{
    int[] arr = { 2, 3, 4, 6 };
    int N = arr.Length;
 
    sameProductQuadruples(arr, N);
}
}
 
// This code is contributed by sanjoy_62.


输出:
8

时间复杂度: O(N 2 )
辅助空间: O(N 2 )