给定N个整数的数组arr [] ,每个数组元素的任务是找到选择一对除当前元素之外的两个相等元素对的方式的数量。
例子:
Input: arr[] = {1, 1, 2, 1, 2}
Output: 2 2 3 2 3
Explanation:
For arr[0] (= 1): The remaining array elements are {1, 2, 1, 2}. Possible choice of pairs are (1, 1), (2, 2). Therefore, count is 2.
For arr[1] (= 1): The remaining array elements are {1, 2, 1, 2}. Therefore, count is 2.
For arr[2] (= 2): The remaining array elements are {1, 1, 1, 2}. Possible choice of pairs are (arr[0], arr[1]), (arr[1], arr[2]) and (arr[0], arr[2]). Therefore, count is 3.
For arr[3] (= 1): The remaining elements are {1, 1, 2, 2}. Therefore, count is 2.
For arr[4] (= 2): The remaining elements are {1, 1, 2, 1}. Therefore, count is 3.
Input: arr[] = {1, 2, 1, 4, 2, 1, 4, 1}
Output: 5 7 5 7 7 5 7 5
天真的方法:解决此问题的最简单方法是遍历每个数组元素的数组,计算剩余数组中所有可能的相等元素对。
时间复杂度: O(N 3 )
辅助空间: O(1)
高效方法:可以基于以下观察条件对上述方法进行优化:对于任何第i个索引(1≤i≤N),计算以下两个值:
- 从数组中选择具有相等值的两个不同元素的方式的数量。
- 从第i个元素以外的N − 1个数组元素中选择一个元素,以使其值与第i个元素的值相同的方式的数目。
请按照以下步骤解决问题:
- 初始化一个映射,例如mp ,以存储每个数组元素的频率。
- 遍历地图以计算由相等值组成的对的数量。将计数存储在一个变量中,例如total 。
- 遍历数组,并为每个第i个索引,打印total –(mp [arr [i]] – 1)作为要求的答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count the number of
// required pairs for every array element
void countEqualElementPairs(int arr[], int N)
{
// Initialize a map
unordered_map mp;
// Update the frequency
// of every element
for (int i = 0; i < N; i++) {
mp[arr[i]] += 1;
}
// Stores the count of pairs
int total = 0;
// Traverse the map
for (auto i : mp) {
// Count the number of ways to
// select pairs consisting of
// equal elements only
total += (i.second * (i.second - 1)) / 2;
}
// Traverse the array
for (int i = 0; i < N; i++) {
// Print the count for
// every array element
cout << total - (mp[arr[i]] - 1)
<< " ";
}
}
// Driver code
int main()
{
// Given array
int arr[] = { 1, 1, 2, 1, 2 };
// Size of the array
int N = sizeof(arr) / sizeof(arr[0]);
countEqualElementPairs(arr, N);
}
Java
/*package whatever //do not write package name here */
import java.io.*;
import java.util.Map;
import java.util.HashMap;
class GFG
{
// Function to count the number of
// required pairs for every array element
public static void countEqualElementPairs(int arr[],
int N)
{
// Initialize a map
HashMap map = new HashMap<>();
// Update the frequency
// of every element
for (int i = 0; i < N; i++)
{
Integer k = map.get(arr[i]);
map.put(arr[i], (k == null) ? 1 : k + 1);
}
// Stores the count of pairs
int total = 0;
// Traverse the map
for (Map.Entry e :
map.entrySet())
{
// Count the number of ways to
// select pairs consisting of
// equal elements only
total
+= (e.getValue() * (e.getValue() - 1)) / 2;
}
// Traverse the array
for (int i = 0; i < N; i++) {
// Print the count for
// every array element
System.out.print(total - (map.get(arr[i]) - 1)
+ " ");
}
}
// Driver code
public static void main(String[] args)
{
// Given array
int arr[] = { 1, 1, 2, 1, 2 };
// Size of the array
int N = 5;
countEqualElementPairs(arr, N);
}
}
// This code is contributed by adity7409.
Python3
# Python3 program for the above approach
# Function to count the number of
# required pairs for every array element
def countEqualElementPairs(arr, N):
# Initialize a map
mp = {}
# Update the frequency
# of every element
for i in range(N):
if arr[i] in mp:
mp[arr[i]] += 1
else:
mp[arr[i]] = 1
# Stores the count of pairs
total = 0
# Traverse the map
for key,value in mp.items():
# Count the number of ways to
# select pairs consisting of
# equal elements only
total += (value * (value - 1)) / 2
# Traverse the array
for i in range(N):
# Print the count for
# every array element
print(int(total - (mp[arr[i]] - 1)),end = " ")
# Driver code
if __name__ == '__main__':
# Given array
arr = [1, 1, 2, 1, 2]
# Size of the array
N = len(arr)
countEqualElementPairs(arr, N)
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to count the number of
// required pairs for every array element
static void countEqualElementPairs(int[] arr, int N)
{
// Initialize a map
Dictionary map = new Dictionary();
// Update the frequency
// of every element
for(int i = 0; i < N; i++)
{
if (!map.ContainsKey(arr[i]))
map[arr[i]] = 1;
else
map[arr[i]]++;
}
// Stores the count of pairs
int total = 0;
// Traverse the map
foreach(KeyValuePair e in map)
{
// Count the number of ways to
// select pairs consisting of
// equal elements only
total += (e.Value * (e.Value - 1)) / 2;
}
// Traverse the array
for(int i = 0; i < N; i++)
{
// Print the count for
// every array element
Console.Write(total - (map[arr[i]] - 1) + " ");
}
}
// Driver code
public static void Main()
{
// Given array
int[] arr = { 1, 1, 2, 1, 2 };
// Size of the array
int N = 5;
countEqualElementPairs(arr, N);
}
}
// This code is contributed by ukasp
2 2 3 2 3
时间复杂度: O(N)
辅助空间: O(N)