📜  计算其产品存在于数组中的对

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

给定一个数组,计算其乘积值出现在数组中的那些对。
例子:

Input : arr[] = {6, 2, 4, 12, 5, 3}
Output : 3
       All pairs whose product exist in array 
       (6 , 2) (2, 3) (4, 3)   

Input :  arr[] = {3, 5, 2, 4, 15, 8}
Output : 2 

一个简单的解决方案是生成所有给定数组对并检查数组中是否存在积。如果存在,则增加计数。最后返回计数。
以下是上述想法的实现

C++
// C++ program to count pairs whose product exist in array
#include
using namespace std;
 
// Returns count of pairs whose product exists in arr[]
int countPairs( int arr[] ,int n)
{
    int result = 0;
    for (int i = 0; i < n ; i++)
    {
        for (int j = i+1 ; j < n ; j++)
        {
            int product = arr[i] * arr[j] ;
 
            // find product in an array
            for (int k = 0; k < n; k++)
            {
                // if product found increment counter
                if (arr[k] == product)
                {
                    result++;
                    break;
                }
            }
        }
    }
 
    // return Count of all pair whose product exist in array
    return result;
}
 
//Driver program
int main()
{
    int arr[] = {6 ,2 ,4 ,12 ,5 ,3} ;
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << countPairs(arr, n);
    return 0;
}


Java
// Java program to count pairs
// whose product exist in array
import java.io.*;
 
class GFG
{
     
// Returns count of pairs
// whose product exists in arr[]
static int countPairs(int arr[],
                      int n)
{
    int result = 0;
    for (int i = 0; i < n ; i++)
    {
        for (int j = i + 1 ; j < n ; j++)
        {
            int product = arr[i] * arr[j] ;
 
            // find product
            // in an array
            for (int k = 0; k < n; k++)
            {
                // if product found
                // increment counter
                if (arr[k] == product)
                {
                    result++;
                    break;
                }
            }
        }
    }
 
    // return Count of all pair
    // whose product exist in array
    return result;
}
 
// Driver Code
public static void main (String[] args)
{
int arr[] = {6, 2, 4, 12, 5, 3} ;
int n = arr.length;
System.out.println(countPairs(arr, n));
}
}
 
// This code is contributed by anuj_67.


Python 3
# Python program to count pairs whose
# product exist in array
 
# Returns count of pairs whose
# product exists in arr[]
def countPairs(arr, n):
 
    result = 0;
    for i in range (0, n):
 
        for j in range(i + 1, n):
             
            product = arr[i] * arr[j] ;
 
            # find product in an array
            for k in range (0, n):
         
                # if product found increment counter
                if (arr[k] == product):
                    result = result + 1;
                    break;
 
    # return Count of all pair whose
    # product exist in array
    return result;
 
# Driver program
arr = [6, 2, 4, 12, 5, 3] ;
n = len(arr);
print(countPairs(arr, n));
     
# This code is contributed
# by Shivi_Aggarwal


C#
// C# program to count pairs
// whose product exist in array
using System;
 
class GFG
{
 
// Returns count of pairs
// whose product exists in arr[]
public static int countPairs(int[] arr,
                             int n)
{
    int result = 0;
    for (int i = 0; i < n ; i++)
    {
        for (int j = i + 1 ; j < n ; j++)
        {
            int product = arr[i] * arr[j];
 
            // find product in an array
            for (int k = 0; k < n; k++)
            {
                // if product found
                // increment counter
                if (arr[k] == product)
                {
                    result++;
                    break;
                }
            }
        }
    }
 
    // return Count of all pair
    // whose product exist in array
    return result;
}
 
// Driver Code
public static void Main(string[] args)
{
    int[] arr = new int[] {6, 2, 4, 12, 5, 3};
    int n = arr.Length;
    Console.WriteLine(countPairs(arr, n));
}
}
 
// This code is contributed by Shrikant13


PHP


Javascript


C++
// A hashing based C++ program to count pairs whose product
// exists in arr[]
#include
using namespace std;
 
// Returns count of pairs whose product exists in arr[]
int countPairs(int arr[] , int n)
{
    int result = 0;
 
    // Create an empty hash-set that store all array element
    set< int > Hash;
 
    // Insert all array element into set
    for (int i = 0 ; i < n; i++)
        Hash.insert(arr[i]);
 
    // Generate all pairs and check is exist in 'Hash' or not
    for (int i = 0 ; i < n; i++)
    {
        for (int j = i + 1; j


Java
// A hashing based Java program to count pairs whose product
// exists in arr[]
import java.util.*;
 
class GFG
{
 
    // Returns count of pairs whose product exists in arr[]
    static int countPairs(int arr[], int n) {
        int result = 0;
 
        // Create an empty hash-set that store all array element
        HashSet< Integer> Hash = new HashSet<>();
 
        // Insert all array element into set
        for (int i = 0; i < n; i++)
        {
            Hash.add(arr[i]);
        }
 
        // Generate all pairs and check is exist in 'Hash' or not
        for (int i = 0; i < n; i++)
        {
            for (int j = i + 1; j < n; j++)
            {
                int product = arr[i] * arr[j];
 
                // if product exists in set then we increment
                // count by 1
                if (Hash.contains(product))
                {
                    result++;
                }
            }
        }
 
        // return count of pairs whose product exist in array
        return result;
    }
 
    // Driver program
    public static void main(String[] args)
    {
        int arr[] = {6, 2, 4, 12, 5, 3};
        int n = arr.length;
        System.out.println(countPairs(arr, n));
    }
}
 
// This code has been contributed by 29AjayKumar


Python3
# A hashing based C++ program to count
# pairs whose product exists in arr[]
 
# Returns count of pairs whose product
# exists in arr[]
def countPairs(arr, n):
    result = 0
 
    # Create an empty hash-set that
    # store all array element
    Hash = set()
 
    # Insert all array element into set
    for i in range(n):
        Hash.add(arr[i])
 
    # Generate all pairs and check is
    # exist in 'Hash' or not
    for i in range(n):
        for j in range(i + 1, n):
            product = arr[i] * arr[j]
 
            # if product exists in set then
            # we increment count by 1
            if product in(Hash):
                result += 1
     
    # return count of pairs whose
    # product exist in array
    return result
 
# Driver Code
if __name__ == '__main__':
    arr = [6, 2, 4, 12, 5, 3]
    n = len(arr)
    print(countPairs(arr, n))
     
# This code is contributed by
# Sanjit_Prasad


C#
// A hashing based C# program to count pairs whose product
// exists in arr[]
using System;
using System.Collections.Generic;
 
class GFG
{
 
    // Returns count of pairs whose product exists in arr[]
    static int countPairs(int []arr, int n)
    {
        int result = 0;
 
        // Create an empty hash-set that store all array element
        HashSet Hash = new HashSet();
 
        // Insert all array element into set
        for (int i = 0; i < n; i++)
        {
            Hash.Add(arr[i]);
        }
 
        // Generate all pairs and check is exist in 'Hash' or not
        for (int i = 0; i < n; i++)
        {
            for (int j = i + 1; j < n; j++)
            {
                int product = arr[i] * arr[j];
 
                // if product exists in set then we increment
                // count by 1
                if (Hash.Contains(product))
                {
                    result++;
                }
            }
        }
 
        // return count of pairs whose product exist in array
        return result;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int []arr = {6, 2, 4, 12, 5, 3};
        int n = arr.Length;
        Console.WriteLine(countPairs(arr, n));
    }
}
 
/* This code contributed by PrinciRaj1992 */


Javascript


输出:

3

时间复杂度: O(n 3 )
一个有效的解决方案是使用存储所有数组元素的“哈希”。生成给定数组 ‘arr’ 的所有可能对,并检查每对的乘积是否在 ‘hash’ 中。如果存在,则增加计数。最后返回计数。
以下是上述想法的实现

C++

// A hashing based C++ program to count pairs whose product
// exists in arr[]
#include
using namespace std;
 
// Returns count of pairs whose product exists in arr[]
int countPairs(int arr[] , int n)
{
    int result = 0;
 
    // Create an empty hash-set that store all array element
    set< int > Hash;
 
    // Insert all array element into set
    for (int i = 0 ; i < n; i++)
        Hash.insert(arr[i]);
 
    // Generate all pairs and check is exist in 'Hash' or not
    for (int i = 0 ; i < n; i++)
    {
        for (int j = i + 1; j

Java

// A hashing based Java program to count pairs whose product
// exists in arr[]
import java.util.*;
 
class GFG
{
 
    // Returns count of pairs whose product exists in arr[]
    static int countPairs(int arr[], int n) {
        int result = 0;
 
        // Create an empty hash-set that store all array element
        HashSet< Integer> Hash = new HashSet<>();
 
        // Insert all array element into set
        for (int i = 0; i < n; i++)
        {
            Hash.add(arr[i]);
        }
 
        // Generate all pairs and check is exist in 'Hash' or not
        for (int i = 0; i < n; i++)
        {
            for (int j = i + 1; j < n; j++)
            {
                int product = arr[i] * arr[j];
 
                // if product exists in set then we increment
                // count by 1
                if (Hash.contains(product))
                {
                    result++;
                }
            }
        }
 
        // return count of pairs whose product exist in array
        return result;
    }
 
    // Driver program
    public static void main(String[] args)
    {
        int arr[] = {6, 2, 4, 12, 5, 3};
        int n = arr.length;
        System.out.println(countPairs(arr, n));
    }
}
 
// This code has been contributed by 29AjayKumar

蟒蛇3

# A hashing based C++ program to count
# pairs whose product exists in arr[]
 
# Returns count of pairs whose product
# exists in arr[]
def countPairs(arr, n):
    result = 0
 
    # Create an empty hash-set that
    # store all array element
    Hash = set()
 
    # Insert all array element into set
    for i in range(n):
        Hash.add(arr[i])
 
    # Generate all pairs and check is
    # exist in 'Hash' or not
    for i in range(n):
        for j in range(i + 1, n):
            product = arr[i] * arr[j]
 
            # if product exists in set then
            # we increment count by 1
            if product in(Hash):
                result += 1
     
    # return count of pairs whose
    # product exist in array
    return result
 
# Driver Code
if __name__ == '__main__':
    arr = [6, 2, 4, 12, 5, 3]
    n = len(arr)
    print(countPairs(arr, n))
     
# This code is contributed by
# Sanjit_Prasad

C#

// A hashing based C# program to count pairs whose product
// exists in arr[]
using System;
using System.Collections.Generic;
 
class GFG
{
 
    // Returns count of pairs whose product exists in arr[]
    static int countPairs(int []arr, int n)
    {
        int result = 0;
 
        // Create an empty hash-set that store all array element
        HashSet Hash = new HashSet();
 
        // Insert all array element into set
        for (int i = 0; i < n; i++)
        {
            Hash.Add(arr[i]);
        }
 
        // Generate all pairs and check is exist in 'Hash' or not
        for (int i = 0; i < n; i++)
        {
            for (int j = i + 1; j < n; j++)
            {
                int product = arr[i] * arr[j];
 
                // if product exists in set then we increment
                // count by 1
                if (Hash.Contains(product))
                {
                    result++;
                }
            }
        }
 
        // return count of pairs whose product exist in array
        return result;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int []arr = {6, 2, 4, 12, 5, 3};
        int n = arr.Length;
        Console.WriteLine(countPairs(arr, n));
    }
}
 
/* This code contributed by PrinciRaj1992 */

Javascript


输出:

3

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