📌  相关文章
📜  计算每次删除后剩余的相等数组元素对

📅  最后修改于: 2021-10-27 08:56:43             🧑  作者: Mango

给定一个大小为N的数组arr[] ,每个数组元素arr[i]的任务是计算通过从数组中删除arr[i]可以获得的相等元素对的数量。

例子:

朴素的方法:解决这个问题的最简单的方法是遍历数组,对于每个i元素,从数组中删除arr[i]并打印数组中剩余的相等数组元素对的计数。

时间复杂度: O(N 2 )
辅助空间: O(N)

有效方法:按照以下步骤解决问题:

  • 初始化一个映射,比如mp ,以存储数组中每个不同元素的频率。
  • 初始化一个变量,比如cntPairs ,以存储相等数组元素对的总数。
  • 遍历映射并通过将cntPairs的值增加(mp[i] * (mp[i] – 1)) / 2 来存储相等元素对的总数。
  • 最后,遍历数组。对于每个i元素,打印(cntPairs – mp[i] + 1) 的值,该值表示通过从数组中删除arr[i]来表示相等数组元素对的计数。

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to count pairs of equal elements
// by removing arr[i] from the array
void pairs_after_removing(int arr[], int N)
{
    // Stores total count of
    // pairs of equal elements
    int cntPairs = 0;
 
    // Store frequency of each
    // distinct array element
    unordered_map mp;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // Update frequency of arr[i]
        mp[arr[i]]++;
    }
 
    // Traverse the map
    for (auto element : mp) {
 
        // Stores key of an element
        int i = element.first;
        cntPairs += mp[i] * (mp[i] - 1) / 2;
    }
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // Stores count of pairs of equal
        // element by removing arr[i]
        int pairs_after_arr_i_removed
            = cntPairs + 1 - mp[arr[i]];
 
        cout << pairs_after_arr_i_removed << ' ';
    }
    return;
}
 
// Driver Code
int main()
{
    // Given Array
    int arr[] = { 2, 3, 4, 3, 2 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    pairs_after_removing(arr, N);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
  
class GFG{
  
// Function to count pairs of equal elements
// by removing arr[i] from the array
static void pairs_after_removing(int arr[], int N)
{
     
    // Stores total count of
    // pairs of equal elements
    int cntPairs = 0;
  
    // Store frequency of each
    // distinct array element
    Map mp = new HashMap();
  
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
         
        // Update frequency of arr[i]
        mp.put(arr[i], mp.getOrDefault(arr[i], 0) + 1);
    }
  
    // Traverse the map
    for(Map.Entry element : mp.entrySet())
    {
         
        // Stores key of an element
        int i = element.getKey();
        cntPairs += mp.get(i) * (mp.get(i) - 1) / 2;
    }
  
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
         
        // Stores count of pairs of equal
        // element by removing arr[i]
        int pairs_after_arr_i_removed = cntPairs +
                           1 - mp.get(arr[i]);
  
        System.out.print(pairs_after_arr_i_removed + " ");
    }
    return;
}
  
// Driver code
public static void main(String[] args)
{
     
    // Given Array
    int arr[] = { 2, 3, 4, 3, 2 };
    int N = arr.length;
  
    pairs_after_removing(arr, N);
}
}
 
// This code is contributed by susmitakundugoaldanga


Python3
# python program to implement
# the above approach
 
# Function to count pairs of equal elements
# by removing arr[i] from the array
def pairs_after_removing(arr, N):
     
    # Stores total count of
    # pairs of equal elements
    cntPairs = 0
 
    # Store frequency of each
    # distinct array element
    mp = {}
 
    # Traverse the array
    for i in arr:
 
        # Update frequency of arr[i]
        mp[i] = mp.get(i, 0) + 1
 
    # Traverse the map
    for element in mp:
 
        # Stores key of an element
        i = element
        cntPairs += mp[i] * (mp[i] - 1) // 2
 
    # Traverse the array
    for i in range(N):
 
        # Stores count of pairs of equal
        # element by removing arr[i]
        pairs_after_arr_i_removed = cntPairs + 1 - mp[arr[i]]
 
        print(pairs_after_arr_i_removed, end = ' ')
    return
 
# Driver Code
if __name__ == '__main__':
   
    # Given Array
    arr = [2, 3, 4, 3, 2]
    N = len(arr)
    pairs_after_removing(arr, N)
 
# This code is contributed by mohit kumar 29


C#
// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
   
class GFG
{
   
// Function to count pairs of equal elements
// by removing arr[i] from the array
static void pairs_after_removing(int[] arr, int N)
{
      
    // Stores total count of
    // pairs of equal elements
    int cntPairs = 0;
   
    // Store frequency of each
    // distinct array element
     Dictionary mp = new Dictionary();
   
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
          
        // Update frequency of arr[i]
        if(mp.ContainsKey(arr[i]))
            {
                mp[arr[i]]++;
            }
            else
            {
                mp[arr[i]] = 1;
            }
    }
   
    // Traverse the map
    foreach(KeyValuePair element in mp)
    {
          
        // Stores key of an element
        int i = element.Key;
        cntPairs += mp[i] * (mp[i] - 1) / 2;
    }
   
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
          
        // Stores count of pairs of equal
        // element by removing arr[i]
        int pairs_after_arr_i_removed = cntPairs +
                           1 - mp[arr[i]];
   
        Console.Write(pairs_after_arr_i_removed + " ");
    }
    return;
}
   
// Driver code
public static void Main()
{
      
    // Given Array
    int[] arr = { 2, 3, 4, 3, 2 };
    int N = arr.Length;
   
    pairs_after_removing(arr, N);
}
}
 
// This code is contributed by sanjoy_62


Javascript


输出:
1 1 2 1 1

时间复杂度: O(N)
辅助空间: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程