📜  作为对或元素平方的乘积的元素计数

📅  最后修改于: 2021-06-26 21:56:12             🧑  作者: Mango

1. 将给定数组按升序排序。
2. 现在，遍历数组，对于每个元素，找到该元素的所有因数对，并使用Binary Search检查数组中是否存在任何对。
3. 对数组进行排序使我们能够在搜索因子的同时执行二进制搜索，从而将计算复杂度降低到O(logN)
4. 如果找到任何这样的对，则增加计数并移至下一个元素，然后重复相同的过程。
5. 打印数组中所有此类数字的最终计数

##### C++
``````// C++ Program to implement the
// above approach
#include
using namespace std;

// Stores all factors a number
vector v[100000];

// Function to calculate and
// store in a vector
void div(int n)
{
for (int i = 2; i <= sqrt(n);
i++) {

if (n % i == 0) {
v[n].push_back(i);
}
}
}

// Function to return the count of
// array elements which are a
// product of two array elements
int prodof2elements(int arr[], int n)
{
int arr2[n];

// Copy elements into a
// a duplicate array
for (int i = 0; i < n; i++) {
arr2[i] = arr[i];
}

// Sort the duplicate array
sort(arr2, arr2 + n);

// Store the count of elements
int ans = 0;

for (int i = 0; i < n; i++) {

// If the factors are not
if (v[arr[i]].size() == 0)
div(arr[i]);

// Traverse its factors
for (auto j : v[arr[i]]) {

// If a pair of
// factors is found
if (binary_search(
arr2, arr2 + n, j)
and binary_search(
arr2, arr2 + n,
arr[i] / j)) {
ans++;
break;
}
}
}

return ans;
}

// Driver Code
int main()
{
int arr[] = { 2, 1, 8, 4, 32, 18 };

int N = sizeof(arr) / sizeof(arr[0]);

cout << prodof2elements(arr, N);

return 0;
}``````

##### Java
``````// Java Program to implement the
// above approach
import java.util.*;
class GFG{

// Stores all factors a number
static Vector[] v =
new Vector[100000];

// Function to calculate and
// store in a vector
static void div(int n)
{
for (int i = 2;
i <= Math.sqrt(n); i++)
{
if (n % i == 0)
{
}
}
}

// Function to return the count of
// array elements which are a
// product of two array elements
static int prodof2elements(int arr[],
int n)
{
int []arr2 = new int[n];

// Copy elements into a
// a duplicate array
for (int i = 0; i < n; i++)
{
arr2[i] = arr[i];
}

// Sort the duplicate
// array
Arrays.sort(arr2);

// Store the count
// of elements
int ans = 0;

for (int i = 0; i < n; i++)
{
// If the factors are not
if (v[arr[i]].size() == 0)
div(arr[i]);

// Traverse its factors
for (int j : v[arr[i]])
{
// If a pair of
// factors is found
if (Arrays.binarySearch(arr2, j) >= 0 &&
Arrays.binarySearch(arr2,
(int)arr[i] / j) >= 0)
{
ans++;
break;
}
}
}

return ans;
}

// Driver Code
public static void main(String[] args)
{
int arr[] = {2, 1, 8, 4, 32, 18};
int N = arr.length;

for (int i = 0; i < v.length; i++)
v[i] = new Vector();

System.out.print(prodof2elements(arr, N));
}
}

// This code is contributed by Princi Singh``````

##### Python3
``````# Python3 program to implement the
# above approach
import math

# Stores all factors a number
v = [[] for i in range(100000)]

# Function to calculate and
# store in a vector
def div(n):

global v

for i in range(2, int(math.sqrt(n)) + 1):
if (n % i == 0):
v[n].append(i)

# Function to return the count of
# array elements which are a
# product of two array elements
def prodof2elements(arr, n):

# Copy elements into a
# a duplicate array
arr2 = arr.copy()

# Sort the duplicate array
arr2.sort()

# Store the count of elements
ans = 0

for i in range(n):

# If the factors are not
if (len(v[arr[i]]) == 0):
div(arr[i])

# Traverse its factors
for j in v[arr[i]]:

# If a pair of
# factors is found
if j in arr2:
if int(arr[i] / j) in arr2:
ans += 1
break

return ans

# Driver Code
arr = [ 2, 1, 8, 4, 32, 18 ]
N = len(arr)

print(prodof2elements(arr, N))

# This code is contributed by avanitrachhadiya2155``````

##### C#
``````// C# Program to implement the
// above approach
using System;
using System.Collections.Generic;
class GFG{

// Stores all factors a number
static List[] v =
new List[100000];

// Function to calculate and
// store in a vector
static void div(int n)
{
for (int i = 2;
i <= Math.Sqrt(n); i++)
{
if (n % i == 0)
{
}
}
}

// Function to return the count of
// array elements which are a
// product of two array elements
static int prodof2elements(int []arr,
int n)
{
int []arr2 = new int[n];

// Copy elements into a
// a duplicate array
for (int i = 0; i < n; i++)
{
arr2[i] = arr[i];
}

// Sort the duplicate
// array
Array.Sort(arr2);

// Store the count
// of elements
int ans = 0;

for (int i = 0; i < n; i++)
{
// If the factors are not
if (v[arr[i]].Count == 0)
div(arr[i]);

// Traverse its factors
foreach (int j in v[arr[i]])
{
// If a pair of
// factors is found
if (Array.BinarySearch(arr2, j) >= 0 &&
Array.BinarySearch(arr2,
(int)arr[i] / j) >= 0)
{
ans++;
break;
}
}
}

return ans;
}

// Driver Code
public static void Main(String[] args)
{
int []arr = {2, 1, 8, 4, 32, 18};
int N = arr.Length;

for (int i = 0; i < v.Length; i++)
v[i] = new List();

Console.Write(prodof2elements(arr, N));
}
}

// This code is contributed by Amit Katiyar``````

``3``