📜  找出数组中满足 ab = cd 的所有对 (a,b) 和 (c,d)

📅  最后修改于: 2021-10-27 07:05:45             🧑  作者: Mango

给定一个不同的整数数组,任务是找到两对 (a, b) 和 (c, d) 使得 ab = cd,其中 a、b、c 和 d 是不同的元素。
例子:

Input  : arr[] = {3, 4, 7, 1, 2, 9, 8}
Output : 4 2 and 1 8
Product of 4 and 2 is 8 and 
also product of 1 and 8 is 8 .

Input  : arr[] = {1, 6, 3, 9, 2, 10};
Output : 6 3 and 9 2

一个简单的解决方案是运行四个循环来生成数组元素的所有可能的四元组。对于每个四元组 (a, b, c, d),检查是否 a*b = c*d。该解决方案的时间复杂度为 O(n 4 )。
此问题的有效解决方案是使用散列。我们使用 product 作为键,pair 作为哈希表中的值。

1. For i=0 to n-1
2.   For j=i+1 to n-1
       a) Find  prod = arr[i]*arr[j]       
       b) If prod is not available in hash then make 
            H[prod] = make_pair(i, j) // H is hash table
       c) If product is also available in hash 
          then print previous and current elements
          of array 
C++
// C++ program to find four elements a, b, c
// and d in array such that ab = cd
#include
using namespace std;
 
// Function to find out four elements in array
// whose product is ab = cd
void findPairs(int arr[], int n)
{
    bool found = false;
    unordered_map > H;
    for (int i=0; i pp = H[prod];
                cout << arr[pp.first] << " " << arr[pp.second]
                     << " and " << arr[i]<<" "<


Java
// Java program to find four elements a, b, c
// and d in array such that ab = cd
import java.io.*;
import java.util.*;
 
class GFG {
     
    public static class pair {
         
        int first,second;
         
        pair(int f, int s)
        {
            first = f;
            second = s;
        }
    };
     
    // Function to find out four elements
    // in array whose product is ab = cd
    public static void findPairs(int arr[], int n)
    {
         
        boolean found = false;
        HashMap hp =
                     new HashMap();
         
        for(int i = 0; i < n; i++)
        {
            for(int j = i + 1; j < n; j++)
            {
                 
                // If product of pair is not in
                // hash table, then store it
                int prod = arr[i] * arr[j];
                 
                if(!hp.containsKey(prod))
                    hp.put(prod, new pair(i,j));
                 
                // If product of pair is also
                // available in then print
                // current and previous pair
                else
                {
                    pair p = hp.get(prod);
                    System.out.println(arr[p.first]
                              + " " + arr[p.second]
                              + " " + "and" + " " +
                             arr[i] + " " + arr[j]);
                    found = true;
                }
            }
        }
         
        // If no pair find then print not found
        if(found == false)
        System.out.println("No pairs Found");
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
        int n = arr.length;
        findPairs(arr, n);   
    }
}
 
// This code is contributed by akash1295.


Python3
# Python3 program to find four elements
# a, b, c and d in array such that ab = cd
 
# Function to find out four elements in array
# whose product is ab = cd
def findPairs(arr, n):
 
    found = False
    H = dict()
 
    for i in range(n):
 
        for j in range(i + 1, n):
         
            # If product of pair is not in hash table,
            # then store it
            prod = arr[i] * arr[j]
 
            if (prod not in H.keys()):
                H[prod] = [i, j]
 
            # If product of pair is also available in
            # then prcurrent and previous pair
            else:
             
                pp = H[prod]
                print(arr[pp[0]], arr[pp[1]],
                      "and", arr[i], arr[j])
                found = True
     
    # If no pair find then prnot found
    if (found == False):
        print("No pairs Found")
 
# Driver code
arr = [1, 2, 3, 4, 5, 6, 7, 8]
n = len(arr)
findPairs(arr, n)
 
# This code is contributed
# by mohit kumar


C#
// C# program to find four elements a, b, c
// and d in array such that ab = cd
using System;
using System.Collections.Generic;
 
class GFG
{
     
    public class pair
    {
         
        public int first,second;
         
        public pair(int f, int s)
        {
            first = f;
            second = s;
        }
    };
     
    // Function to find out four elements
    // in array whose product is ab = cd
    public static void findPairs(int[] arr, int n)
    {
         
        bool found = false;
        Dictionary hp =
                    new Dictionary();
         
        for(int i = 0; i < n; i++)
        {
            for(int j = i + 1; j < n; j++)
            {
                 
                // If product of pair is not in
                // hash table, then store it
                int prod = arr[i] * arr[j];
                 
                if(!hp.ContainsKey(prod))
                    hp.Add(prod, new pair(i,j));
                 
                // If product of pair is also
                // available in then print
                // current and previous pair
                else
                {
                    pair p = hp[prod];
                    Console.WriteLine(arr[p.first]
                            + " " + arr[p.second]
                            + " " + "and" + " " +
                            arr[i] + " " + arr[j]);
                    found = true;
                }
            }
        }
         
        // If no pair find then print not found
        if(found == false)
        Console.WriteLine("No pairs Found");
    }
     
    // Driver code
    public static void Main (String[] args)
    {
        int []arr = {1, 2, 3, 4, 5, 6, 7, 8};
        int n = arr.Length;
        findPairs(arr, n);
    }
}
 
/* This code contributed by PrinciRaj1992 */


Javascript


输出:

1 6 and 2 3
1 8 and 2 4
2 6 and 3 4
3 8 and 4 6

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程