📌  相关文章
📜  一对乘积等于剩余对乘积的四元组计数

📅  最后修改于: 2021-09-02 07:37:17             🧑  作者: Mango

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

例子:

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

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

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

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

  • res初始化为0以存储结果四元组的计数。
  • 创建一个哈希图M来存储数组中不同对的乘积频率。
  • 现在,生成给定数组的所有可能的不同对(arr[i], arr[j])并执行以下操作:
    • arr[i]arr[j]的乘积存储在变量prod 中
    • (8*M[prod])的值添加到变量res
    • 将 hashmap 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.


Javascript


输出:
8

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live