给定N个正整数的数组arr [] ,任务是计算可以表示为两个不同数组元素的乘积或任何数组元素的平方的数组元素的数量。
例子:
Input: N = 5, arr[] = {3, 2, 6, 18, 4}
Output: 3
Explanation:
3 elements from the given array 6, 18 and 4 can be expressed as {2 * 3, 6 * 3, 2 * 2} respectively.
Input: N = 6, arr[] = {5, 15, 3, 7, 10, 17}
Output: 1
Explanation:
Only 15 can be expressed as 5 * 3.
天真的方法:
最简单的方法是遍历[0,N-1]范围内的数组,并对索引值i在给定范围内的每个数字,在同一数组上运行嵌套循环,以找出两个乘积为arr [一世]。如果找到一对数字,则在对应的索引上打印1,否则打印0。
时间复杂度: O(N 3 )
高效方法:
为了解决这个问题,我们需要找到每个元素的所有因子,并且对于每个元素,检查数组中是否存在该元素的一对因子。请按照以下步骤解决问题:
- 将给定数组按升序排序。
- 现在,遍历数组,对于每个元素,找到该元素的所有因数对,并使用Binary Search检查数组中是否存在任何对。
- 对数组进行排序使我们能够在搜索因子的同时执行二进制搜索,从而将计算复杂度降低到O(logN) 。
- 如果找到任何这样的对,则增加计数并移至下一个元素,然后重复相同的过程。
- 打印数组中所有此类数字的最终计数。
下面是上述方法的实现:
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
// calculated already
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)
{
v[n].add(i);
}
}
}
// 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
// calculated already
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
# calculated already
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)
{
v[n].Add(i);
}
}
}
// 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
// calculated already
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
时间复杂度: O(N 3/2 * log N)
辅助空间: O(N)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。