给定一个由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
Javascript
2 2 3 2 3
时间复杂度: O(N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。