用产品 K 计算给定数组中所有可能的对
给定一个整数数组 arr[]大小为N和一个正整数K ,任务是计算数组中的所有对,乘积等于K 。
例子:
Input: arr[] = {1, 2, 16, 4, 4, 4, 8 }, K=16
Output: 5
Explanation: Possible pairs are (1, 16), (2, 8), (4, 4), (4, 4), (4, 4)
Input: arr[] = {1, 10, 20, 10, 4, 5, 5, 2 }, K=20
Output: 5
Explanation: Possible pairs are (1, 20), (2, 10), (2, 10), (4, 5), (4, 5)
方法:想法是使用散列来存储元素并检查数组中是否存在K/arr[i]或不使用映射并相应地增加计数。
请按照以下步骤解决问题:
- 将变量count初始化为0以存储答案。
- 初始化 unordered_map
mp[] 。 - 使用变量i在范围[0, N)上进行迭代,并将数组arr[]的所有元素的频率存储在映射mp[] 中。
- 使用变量i遍历范围[0, N)并执行以下任务:
- 将变量索引初始化为K/arr[i] 。
- 如果K不是2的幂并且映射mp[]中存在索引,则将count的值增加mp[arr[i]]*mp[index]并从映射mp[] 中删除它们。
- 如果K是2的幂并且索引存在于映射mp[]中,则将count的值增加mp[index]*(mp[index]-1)/2并将其从映射mp[] 中删除。
- 执行上述步骤后,打印count的值作为答案。
下面是上述方法的实现。
C++14
// C++ program for the above approach
#include
using namespace std;
// Function to count
// the total number of pairs
int countPairsWithProductK(
int arr[], int n, int k)
{
int count = 0;
// Initialize hashmap.
unordered_map mp;
// Insert array elements to hashmap
for (int i = 0; i < n; i++) {
mp[arr[i]]++;
}
for (int i = 0; i < n; i++) {
double index = 1.0 * k / arr[i];
// If k is not power of two
if (index >= 0
&& ((index - (int)(index)) == 0)
&& mp.find(k / arr[i]) != mp.end()
&& (index != arr[i])) {
count += mp[arr[i]] * mp[index];
// After counting erase the element
mp.erase(arr[i]);
mp.erase(index);
}
// If k is power of 2
if (index >= 0
&& ((index - (int)(index)) == 0)
&& mp.find(k / arr[i]) != mp.end()
&& (index == arr[i])) {
// Pair count
count += (mp[arr[i]]
* (mp[arr[i]] - 1))
/ 2;
// After counting erase the element;
mp.erase(arr[i]);
}
}
return count;
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 16, 4, 4, 4, 8 };
int N = sizeof(arr) / sizeof(arr[0]);
int K = 16;
cout << countPairsWithProductK(arr, N, K);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to count
// the total number of pairs
static int countPairsWithProductK(
int arr[], int n, int k)
{
int count = 0;
// Initialize hashmap.
HashMap mp = new HashMap();
// Insert array elements to hashmap
for (int i = 0; i < n; i++) {
if(mp.containsKey(arr[i])){
mp.put(arr[i], mp.get(arr[i])+1);
}else{
mp.put(arr[i], 1);
}
}
for (int i = 0; i < n; i++) {
int index = (int) (1.0 * k / arr[i]);
// If k is not power of two
if (index >= 0
&& ((index - (int)(index)) == 0)
&& mp.containsKey(k / arr[i])
&& (index != arr[i])) {
count += mp.get(arr[i]) * mp.get(index);
// After counting erase the element
mp.remove(arr[i]);
mp.remove(index);
}
// If k is power of 2
if (index >= 0
&& ((index - (int)(index)) == 0)
&& mp.containsKey(k / arr[i])
&& (index == arr[i])) {
// Pair count
count += (mp.get(arr[i])
* (mp.get(arr[i]) - 1))
/ 2;
// After counting erase the element;
mp.remove(arr[i]);
}
}
return count;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 1, 2, 16, 4, 4, 4, 8 };
int N = arr.length;
int K = 16;
System.out.print(countPairsWithProductK(arr, N, K));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python 3 program for the above approach
from collections import defaultdict
# Function to count
# the total number of pairs
def countPairsWithProductK(arr, n, k):
count = 0
# Initialize hashmap.
mp = defaultdict(int)
# Insert array elements to hashmap
for i in range(n):
mp[arr[i]] += 1
for i in range(n):
index = 1.0 * k / arr[i]
# If k is not power of two
if (index >= 0
and ((index - (int)(index)) == 0)
and (k / arr[i]) in mp
and (index != arr[i])):
count += mp[arr[i]] * mp[index]
# After counting erase the element
del mp[arr[i]]
del mp[index]
# If k is power of 2
if (index >= 0
and ((index - (int)(index)) == 0)
and (k / arr[i]) in mp
and (index == arr[i])):
# Pair count
count += ((mp[arr[i]]
* (mp[arr[i]] - 1)) / 2)
# After counting erase the element;
del mp[arr[i]]
return count
# Driver Code
if __name__ == "__main__":
arr = [1, 2, 16, 4, 4, 4, 8]
N = len(arr)
K = 16
print(int(countPairsWithProductK(arr, N, K)))
# This code is contributed by ukasp.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG{
// Function to count
// the total number of pairs
static int countPairsWithProductK(
int []arr, int n, int k)
{
int count = 0;
// Initialize hashmap.
Dictionary mp = new Dictionary();
// Insert array elements to hashmap
for (int i = 0; i < n; i++) {
if(mp.ContainsKey(arr[i])){
mp[arr[i]] = mp[arr[i]]+1;
}else{
mp.Add(arr[i], 1);
}
}
for (int i = 0; i < n; i++) {
int index = (int) (1.0 * k / arr[i]);
// If k is not power of two
if (index >= 0
&& ((index - (int)(index)) == 0)
&& mp.ContainsKey(k / arr[i])
&& (index != arr[i])) {
count += mp[arr[i]] * mp[index];
// After counting erase the element
mp.Remove(arr[i]);
mp.Remove(index);
}
// If k is power of 2
if (index >= 0
&& ((index - (int)(index)) == 0)
&& mp.ContainsKey(k / arr[i])
&& (index == arr[i])) {
// Pair count
count += (mp[arr[i]]
* (mp[arr[i]] - 1))
/ 2;
// After counting erase the element;
mp.Remove(arr[i]);
}
}
return count;
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 1, 2, 16, 4, 4, 4, 8 };
int N = arr.Length;
int K = 16;
Console.Write(countPairsWithProductK(arr, N, K));
}
}
// This code is contributed by Rajput-Ji
Javascript
输出
5
时间复杂度: O(N)
辅助空间: O(N)